4

Possible Duplicate:
Rounding numbers in Objective-C
objective -C : how to truncate extra zero in float?
Correcting floating point numbers

In Objective C, I have a double which receives the answer to a calculation as shown:

double answer = a / b;

The double variable can sometimes be a whole integer but can also be a fraction e.g.

NSLog(@"%f", answer)

This can return 2.000000 or 5.142394 or 2.142000.

I was wondering if there was a way to edit the decimal places so that the trailing 0's are not visible for example:

2.000000 would be 2
5.142394 would be 5.142394
2.142000 would be 2.142

How can this be done?

Community
  • 1
  • 1
CoreCode
  • 2,227
  • 4
  • 22
  • 24
  • Wouldn't a.f/b.f limit it to integer values? – CodaFi Apr 19 '12 at 03:51
  • @CodaFi I do not wish to limit it to integer values. It needs to have dec places but not with the excess 0's. This is not a duplicate as I am not trying to round numbers. I never mentioned rounding numbers. – CoreCode Apr 19 '12 at 03:52
  • @lnafziger It is not really as this is not a floating point number issue – CoreCode Apr 19 '12 at 04:11
  • Actually, it's the same issue... A double is a floating point number and the answer addresses the rounding issue that you are asking about. – lnafziger Apr 19 '12 at 04:12
  • @lnafziger Again, It is not a rounding issue. I do, under no circumstances, want the number to be rounded. – CoreCode Apr 19 '12 at 04:14
  • Understood (although you clearly mis-understand how float/double's are stored because you will run into this) but it still covers the truncation of 0's and formatting the way that you are asking about.... – lnafziger Apr 19 '12 at 04:16
  • From a mathematical point of view, you should not cut the trailing zeroes from the floats. 2.00 is a much more precise statement than 2. – thundersteele Apr 19 '12 at 04:31

2 Answers2

10

If you'd like to hard-code the number of decimal places you're after, this would give you two decimal places:

NSLog(@"%.2f", answer);

Read more about format specifiers here:

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

Alternatively, if you'd like to change the number of decimal places on-the-fly, consider using the NSNumberFormatter class:

int maxDigitsAfterDecimal = 3; // here's where you set the dp

NSNumberFormatter * nf = [[NSNumberFormatter alloc] init];
[nf setMaximumFractionDigits:maxDigitsAfterDecimal];

NSString * trimmed = [nf stringFromNumber:[NSNumber numberWithDouble:3.14159]];
NSLog(@"%@", trimmed); // in this case, 3.142

[nf release];
wpearse
  • 2,422
  • 2
  • 29
  • 30
  • I would need it to be dynamic though, so not always 2 dec places – CoreCode Apr 19 '12 at 03:51
  • Oh. In that case, try NSNumberFormatter: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/Reference/Reference.html – wpearse Apr 19 '12 at 03:55
  • If you could re-write your answer with the correct solution, I will give it the green tick – CoreCode Apr 19 '12 at 03:56
  • I've edited my answer to include an NSNumberFormatterExample. I've tested this example just now to make sure it works :) – wpearse Apr 19 '12 at 10:01
2

NSNumberFormatter is exactly what you're looking for. (my apologies if you aren't working with a Mac, I'll post a link to similar iOS docs later).

EDIT (by request): NSNumberFormatter is in fact available for iOS (the docs I posted above are the same for iOS), and has a few relevant properties that you should set. For one, it's numberStyle property should be set to NSNumberFormatterDecimalStyle (others include percent, banker, and spell-out). NSNumberFormatter also includes a roundingMode property, which in your case should be NSNumberFormatterRoundHalfEven. This will change your decimal to have the last digit rounded off, but it's the best way to get rid of those zeroes.

CodaFi
  • 43,043
  • 8
  • 107
  • 153