944,093 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9789
  • C RSS
Dec 4th, 2004
0

setting the number of decimal points in double varibale

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shahin is offline Offline
5 posts
since Dec 2004
Dec 4th, 2004
0

Re: setting the number of decimal points in double varibale

#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
*/
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 4th, 2004
0

Re: setting the number of decimal points in double varibale

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shahin is offline Offline
5 posts
since Dec 2004
Dec 4th, 2004
0

Re: setting the number of decimal points in double varibale

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 4th, 2004
0

Re: setting the number of decimal points in double varibale

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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shahin is offline Offline
5 posts
since Dec 2004
Dec 4th, 2004
0

Re: setting the number of decimal points in double varibale

  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
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: New Tutorials ??
Next Thread in C Forum Timeline: C/ Need Help with Linked Lists please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC