Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show the float value in formatted string two digits after decimal in iPhone

I have a float number in which i insert comma after thousands value it works fine

NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; // this line is important!

NSString *formatted = [formatter stringFromNumber:[NSNumber numberWithFloat:appDelegate.C_WND_ClinInf_Average_Default]];

but it shows 1,947 but i want it to show 1,947.00 any idea how to formate like this.

Thanks

like image 579
Native Developer Avatar asked Dec 09 '22 13:12

Native Developer


1 Answers

Have you tried using -setMaximumFractionDigits from NSNumberFormatter?

So, try something like:

[formatter setMaximumFractionDigits:2];
[formatter setMinimumFractionDigits:2];

This way you deal with NSNumberFormatter only and don't need to call anything extra on NSString.

like image 130
rocir Avatar answered Dec 11 '22 01:12

rocir