Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to fix my double value to 14 digits after decimal point. in order to cmpare with VB i want to fix my double value to 14 digits ..iam doing in mfc.. how to do in mfc.

in VB the double value precision is always 14. but in our mfc its 16. so i want to fix the value to 14.

What I have tried:

double d = 2.483650149760682

i want these value as 2.483650149760
Posted
Updated 1-Jun-17 1:32am

Just print them with the required number of digits after the decimal point:
C++
double d = 2.483650149760682;
// Prints as 2.483650149760 (12 digits after decimal point)
printf("%.12f\n", d);

See printf - C++ Reference[^] for the format options. To print to a buffer (prepare string) use sprintf - C++ Reference[^] or a formatting method of the used string class (if available, e.g. CStringT::Format[^] ).

When using C++ streams use the setprecision - C++ Reference[^] method.
 
Share this answer
 
Comments
Richard MacCutchan 1-Jun-17 7:36am    
The issue is that OP really does not understand Floating point numbers.
You already posted this question at Vb string to double vs mfc cstring to double[^]; please do not repost. I also explained what the issue is and gave you a link to an article that explains floating point numbers. As I said then, if you really do not understand floating point you should avoid using them.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900