ios - How to calculate double value upto 2 decimals, but without rounding off the digits -
i have double number e.g "12.1278", want convert number 2 decimals only, "12.12".
but, when use following code:
double d = 12.1278; nslog(@"float upto 2 decimals : %.2f", d);
the output is:
float upto 2 decimals : 12.13
i want conversion like:
float upto 2 decimals : 12.12
use nsnumberformatter
roundingmode
of nsnumberformatterroundfloor
.
double d = 12.1278; nsnumberformatter *formatter = [nsnumberformatter new]; [formatter setroundingmode:nsnumberformatterroundfloor]; [formatter setmaximumfractiondigits:2]; nsstring *numberstring = [formatter stringfromnumber:@(d)]; nslog(@"numberstring: %@", numberstring);
nslog output:
numberstring: 12.12
Comments
Post a Comment