setting the number of decimal points in double varibale

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2004
Posts: 5
Reputation: shahin is an unknown quantity at this point 
Solved Threads: 0
shahin shahin is offline Offline
Newbie Poster

setting the number of decimal points in double varibale

 
0
  #1
Dec 4th, 2004
Hello All
I am new at this website and I hope I can be a good memeber.
I have a question about C programing language.

I need to change the decimal point of a double for example:
If I have a doubles with 5 decimal digits

123.45678
234.12345
34.34567

I want to be able to change them to only 2 decimal point , for example:

123.45
234.12
34.34

or to 3 decimal point:
123.456
234.123
34.345

I am doing some testing and I want to investigate the result of more decimal digit in the final result. Is there any C function which does that??

Or Is there any easy way to do this??

Thank you guys.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,395
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 245
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: setting the number of decimal points in double varibale

 
0
  #2
Dec 4th, 2004
#include <stdio.h>

int main(void)
{
   double value[] = {123.45678,234.12345,34.34567};
   int j;
   for ( j = 2; j <= 3; ++j )
   {
      size_t i;
      printf("%d decimal places:\n", j);
      for ( i = 0; i < sizeof value / sizeof *value; ++i )
      {
         printf("%.*f\n", j, value[i]);
      }
   }
   return 0;
}

/* my output
2 decimal places:
123.46
234.12
34.35
3 decimal places:
123.457
234.123
34.346
*/
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 5
Reputation: shahin is an unknown quantity at this point 
Solved Threads: 0
shahin shahin is offline Offline
Newbie Poster

Re: setting the number of decimal points in double varibale

 
0
  #3
Dec 4th, 2004
great, so there is no built in function but I can use this simple routine and change the percision.
But This is just priniting the values out.
I need to do calculation with this values, for example if value is 12.34567
then I need to do my calculation with just 12.34
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,395
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 245
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: setting the number of decimal points in double varibale

 
0
  #4
Dec 4th, 2004
Mutliply by 100; cast to int to truncate, then divide by 100 back to a double.
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main(void)
  5. {
  6. double value[] = {123.45678,234.12345,34.34567};
  7. int j;
  8. for ( j = 2; j <= 3; ++j )
  9. {
  10. size_t i;
  11. printf("%d decimal places:\n", j);
  12. for ( i = 0; i < sizeof value / sizeof *value; ++i )
  13. {
  14. double scale = pow(10.0, j);
  15. double result = (int)(value[i] * scale) / scale;
  16. printf("result = %g\n", result);
  17. }
  18. }
  19. return 0;
  20. }
  21.  
  22. /* my output
  23. 2 decimal places:
  24. result = 123.45
  25. result = 234.12
  26. result = 34.34
  27. 3 decimal places:
  28. result = 123.456
  29. result = 234.123
  30. result = 34.345
  31. */
Last edited by Dave Sinkula; Dec 4th, 2004 at 6:47 pm. Reason: Added code.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 5
Reputation: shahin is an unknown quantity at this point 
Solved Threads: 0
shahin shahin is offline Offline
Newbie Poster

Re: setting the number of decimal points in double varibale

 
0
  #5
Dec 4th, 2004
great. thank you sooo much, I can not belive you guys give answer this quick.
another question?

I have tried this for 4 digit of decimal digit and I get the wrong results.
Why is that?? Can I extend this to 4 digit or 5 digit??

Another question is that, If we just want to save a double with 2 dicimal digit, then how many bits do we need?? in other words, what is a relationship between number of digits in dicimal and number of bytes that we use for it. do I make any sense??


Can you suggest a good book or a good website which talks about the socket programing using MFC??
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,395
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 245
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: setting the number of decimal points in double varibale

 
0
  #6
Dec 4th, 2004
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main(void)
  5. {
  6. double value[] = {123.45678,234.12345,34.34567};
  7. int j;
  8. for ( j = 2; j <= 6; ++j )
  9. {
  10. size_t i;
  11. printf("%d decimal places:\n", j);
  12. for ( i = 0; i < sizeof value / sizeof *value; ++i )
  13. {
  14. double scale = pow(10.0, j);
  15. double result = (int)(value[i] * scale) / scale;
  16. printf("result = %f\n", result);
  17. }
  18. }
  19. return 0;
  20. }
  21.  
  22. /* my output
  23. 2 decimal places:
  24. result = 123.450000
  25. result = 234.120000
  26. result = 34.340000
  27. 3 decimal places:
  28. result = 123.456000
  29. result = 234.123000
  30. result = 34.345000
  31. 4 decimal places:
  32. result = 123.456700
  33. result = 234.123400
  34. result = 34.345600
  35. 5 decimal places:
  36. result = 123.456770
  37. result = 234.123440
  38. result = 34.345660
  39. 6 decimal places:
  40. result = 123.456779
  41. result = 234.123449
  42. result = 34.345669
  43. */
What Every Computer Scientist Should Know About Floating-Point Arithmetic
Beej's Guide to Network Programming
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC