18

Let me know how to round decimal for 2 decimal places in Objective-C.

I would like to do like this. (all of numbers following sentence is float value)

• round

10.118 => 10.12

10.114 => 10.11

• ceil

10.118 => 10.12

• floor

10.114 => 10.11

Thanks for checking my question.

Takuya Takahashi
  • 453
  • 1
  • 4
  • 16
  • 3
    You do it the same way that you do it in C :) – Sergey Kalinichenko Mar 15 '13 at 09:55
  • Do you need resulting values to be strings or remain floats? – coverback Mar 15 '13 at 09:56
  • http://stackoverflow.com/questions/10749483/how-to-convert-a-float-value-to-rounded-off-in-iphone-app/10749620#10749620 – Prasad G Mar 15 '13 at 09:59
  • If you work with primitives Anoop is the right answer, if you work with NSNumber object is a little different because you need to specify an NSNumberFormatter object. You can also use the NSDecimalRound function: void NSDecimalRound ( NSDecimal *result, const NSDecimal *number, NSInteger scale, NSRoundingMode roundingMode ); NSDecimal is just an NSNumber subclass. I prefer the latter. – Andrea Mar 15 '13 at 10:07
  • @Andrea NSDecimal is not a subclass of NSNumber. it is not even a class. NSDecimal is a C-struct. you are mixing them up with NSDecimalNumber. – vikingosegundo Mar 15 '13 at 10:11
  • @Andrea https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/c_ref/NSDecimal – vikingosegundo Mar 15 '13 at 10:11
  • You are totally right sorry for the mistake – Andrea Mar 15 '13 at 10:12
  • 1
    I think this is not a duplicate of the given question; this one is about Objective-C, while the marked duplicate is only about C. Perhaps there is a cocoa library that is preferred over standard C libraries? – Ky - Oct 02 '15 at 19:20

3 Answers3

38

If you actually need the number to be rounded, and not just when presenting it:

float roundToN(float num, int decimals)
{
    int tenpow = 1;
    for (; decimals; tenpow *= 10, decimals--);
    return round(tenpow * num) / tenpow;
}

Or always to two decimal places:

float roundToTwo(float num)
{
    return round(100 * num) / 100;
}
17

You can use the below code to format it to two decimal places

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.setMaximumFractionDigits = 2;
formatter.setRoundingMode = NSNumberFormatterRoundUp;

NSString *numberString = [formatter stringFromNumber:@(10.358)];
NSLog(@"Result %@",numberString); // Result 10.36
Ky -
  • 30,724
  • 51
  • 192
  • 308
Suhaiyl
  • 1,071
  • 9
  • 17
  • 1
    I think OP wants to *actually* round the number. –  Mar 15 '13 at 10:01
  • as per condition given by @Takuya (10.118 => 10.12 10.114 => 10.11 • ceil 10.118 => 10.12 • floor 10.114 => 10.11) i think this should work for him. – Suhaiyl Mar 15 '13 at 10:05
  • I mean, he doesn't want to *print* a rounded value but to *have* a rounded value. –  Mar 15 '13 at 10:06
  • Sorry i didnt get what u mean by "Print"/"To have" – Suhaiyl Mar 15 '13 at 10:07
  • Perhaps `.RoundUp` isn't the best rounding mode (e.g. it turns `10.351` to `10.36`); I'd recommend `.RoundHalfUp` (e.g. it turns `10.351` to `10.35`). – Ky - Oct 02 '15 at 20:29
  • @user529758 this can easily be done with [`numberString.doubleValue`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/#//apple_ref/occ/instp/NSString/doubleValue) or even [`[formatter numberFromString:numberString]`](https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/index.html#//apple_ref/occ/instm/NSNumberFormatter/numberFromString:) – Ky - Oct 05 '15 at 13:10
  • NSNumberFormatter will convert "." into "," if your device region is selected differently like Brazil etc. – Haseeb Javed Dec 13 '22 at 12:57
0
float roundedFloat = (int)(sourceFloat * 100 + 0.5) / 100.0;
Gobra
  • 4,263
  • 2
  • 15
  • 20
  • This isn't correct for negative numbers. There's a `round()` function in the C standard library. –  Mar 15 '13 at 10:03
  • True, but: 1. there were only positive numbers in the example, 2. my goal was just to show the idea ;) – Gobra Mar 15 '13 at 10:16
  • 1
    I see your point, but this is not *the idea.* Sorry. –  Mar 15 '13 at 10:17