| | |
setting the number of decimal points in double varibale
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2004
Posts: 5
Reputation:
Solved Threads: 0
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.
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.
#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
*/ Mutliply by 100; cast to int to truncate, then divide by 100 back to a double.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <math.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 ) { double scale = pow(10.0, j); double result = (int)(value[i] * scale) / scale; printf("result = %g\n", result); } } return 0; } /* my output 2 decimal places: result = 123.45 result = 234.12 result = 34.34 3 decimal places: result = 123.456 result = 234.123 result = 34.345 */
Last edited by Dave Sinkula; Dec 4th, 2004 at 6:47 pm. Reason: Added code.
•
•
Join Date: Dec 2004
Posts: 5
Reputation:
Solved Threads: 0
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??


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??
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <math.h> int main(void) { double value[] = {123.45678,234.12345,34.34567}; int j; for ( j = 2; j <= 6; ++j ) { size_t i; printf("%d decimal places:\n", j); for ( i = 0; i < sizeof value / sizeof *value; ++i ) { double scale = pow(10.0, j); double result = (int)(value[i] * scale) / scale; printf("result = %f\n", result); } } return 0; } /* my output 2 decimal places: result = 123.450000 result = 234.120000 result = 34.340000 3 decimal places: result = 123.456000 result = 234.123000 result = 34.345000 4 decimal places: result = 123.456700 result = 234.123400 result = 34.345600 5 decimal places: result = 123.456770 result = 234.123440 result = 34.345660 6 decimal places: result = 123.456779 result = 234.123449 result = 34.345669 */
Beej's Guide to Network Programming
![]() |
Similar Threads
- Regular Expressions with Decimal Points (Java)
- widths for decimal points (C++)
- define variable (C)
- How to remove decimal points from a price... (ASP)
Other Threads in the C Forum
- Previous Thread: New Tutorials ??
- Next Thread: C/ Need Help with Linked Lists please
| Thread Tools | Search this Thread |
#include * api append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database dynamic execv feet fgets file floatingpointvalidation fork framework frequency function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf open openwebfoundation overwrite pdf pointer pointers posix power process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send single socket socketprogramming standard strchr string suggestions test testautomation testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






