iAsh iAsh - 1 year ago 133
iOS Question

Get NSDecimalNumber with 2 decimal places

I want to create NSDecimalNumber after dividing 100. I am using following code:

NSDecimalNumber *amountToSendNumber = [number decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:@"100"]];

It gives me correct value if I provide input as 2536 as 25.36.
But if I provide input as 2500 then output is 25.
I want output as 25.00 without having any rounding off mechanism.

Any Help?

Answer Source

You need to use NSNumberFormatter while you display a NSDecimalNumber

  NSNumberFormatter *formatter = [NSNumberFormatter new];
  [formatter setMinimumFractionDigits:2];
  NSLog(@"%@", [formatter stringFromNumber:amountToSendNumber]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download