I have double number in a format like 34.123456789
.
How can I change it to 34.123?
I want just 3 digits after the decimal point.
I have double number in a format like 34.123456789
.
How can I change it to 34.123?
I want just 3 digits after the decimal point.
You can use:
#include <math.h>
:
dbl = round (dbl * 1000.0) / 1000.0;
Just keep in mind that floats and doubles are as close an approximation as the underlying type (usually IEEE754) can provide. It may not be exact.
You can print it to 3 decimal places with [NSString stringWithFormat:@".3f",d]
.
You can approximately round it with round(d*1000)/1000
, but of course this isn't guaranteed to be exact since 1000 isn't a power of 2.