How do I change the number of decimal places iOS?

前端 未结 5 1288
抹茶落季
抹茶落季 2020-12-10 21:04

I have a simple calculator app and I want it to be so that if the answer requires no decimal places, there are none, just whole numbers. If the answer was 2, I don\'t want i

5条回答
  •  萌比男神i
    2020-12-10 21:44

    One way is to use NSNumberFormatter to format your result instead of NSString's -stringWithFormat::

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setMaximumFractionDigits:requiredDigits];
    [formatter setMinimumFractionDigits:0];
    NSString *result = [formatter stringFromNumber:[NSNumber numberWithFloat:currentNumber];
    

提交回复
热议问题