简体   繁体   中英

Why reading Floating point number from file gives rounded output?

i have a file and two inputs are shown below:

34.800287000 0.077352000

i'm reading from a file(by getline then define stringstream) and saving it in my class variables which are both defined double . However when i check my variables i see that:

34.8003 0.077352

EDIT: i'm using cout to check my variables.

why is that ?

thanks.

The standard IO streams classes have a limit to their precision that can be customized at runtime. By default I believe it's six places, which matches the output you're getting above. If you want to increase the precision, you can use the setprecision stream manipulator:

double myValue = /* ... */
cout << setprecision(12) << myValue << endl; // Print with higher precision

The setprecision manipulator is defined in <iomanip> and when used once will change the behavior of cout to print at higher precision for the rest of the program, which in your case may be helpful. Try this out and see if your numbers really are losing precision.

when printing with cout, your numbers will be rounded. If you want to see more decimals, use std::setprecision from header iomanip : http://www.cplusplus.com/reference/iostream/manipulators/setprecision/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM