C++ significant figures

joshim5 picture joshim5 · Jan 5, 2011 · Viewed 36.5k times · Source

How can I do math involving significant figures in C++? I want this to work correct with measured data from chemistry and physics experiments. An example: 65 / 5 = 10. I would need to get rid of unneeded decimal places and replace some digits with 0s.

Thanks!

Answer

Rion Williams picture Rion Williams · Jan 5, 2011

This should get you what you need:

std::cout.precision(x); // x would be the number of significant figures to output