0

I'm trying to get an NSNumber out of an NSMutableArray that's been previously manipluated as a double and then added to the array to print out in a label (NSString).

It's important that the number stays as an accurate representatoin of a double with no scientific notation to abbreviate the answer.

The other requirement is to have it print to maybe 15 or 16 decimal places, rounding is optional but not required.

I also do not want trailing 0's when displaying the double

I've tried the following but these do not work...

This is ok but ends the number with a . (eg: 1+1=2.)

double test = [[data.argOperands objectAtIndex:0]doubleValue];
label.text = [NSString stringWithFormat:@"%3.2f", test];
label.text = [label.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:[NSString stringWithFormat:@"0"]]];

I then try something like this, which is wrong because if I do 9^99 it'll print inf or 0.0003/4 it'll give scientific numbers instead of the value

    float y = [[calcData.argOperands objectAtIndex:0]doubleValue];;
    label.text = [NSString stringWithFormat:@"%g", y];

If I do the following using double it's getting close, 9^99 works, but 3.33/5 returns 0.666000 with trailing 0's

    double y = [[data.argOperands objectAtIndex:0]doubleValue];
    label.text = [NSString stringWithFormat:@"%f", y];

Any code examples of how to do it this way using either NSNumberFormatter or NSDecimalNumber would be greatly appreciated.

edumike
  • 3,149
  • 7
  • 27
  • 33
  • Try to this way http://stackoverflow.com/questions/15259826/having-trouble-using-nsnumberformatter-for-currency-conversion-in-ios http://stackoverflow.com/questions/19551568/converting-currency-string-into-nsdecimalnumber – Taruna May 26 '14 at 13:58

2 Answers2

0

"%.13f" Would give you 13 decimal places, but that would give you trailing zeros.

You may need to create an NSNumberFormatter and use that.

I suspect you're not going to be happy no matter what. Binary floating point is not an exact representation of decimal. The decimal value .1 is not an exact value in binary, and might display as something like .09999999998 if you display it with 13 decimal places.

You also might look at using NSDecimalNumber, which stores values as decimal digits. It's slower than other ways of doing math but you can control the results exactly.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Can you give code examples of NSNumberFormatter and NSDecimalNumber given an NSNumber to an NSString? – edumike May 26 '14 at 12:25
  • 1
    @edumike here is a good reference to follow for [NSNumberFormatter](http://nshipster.com/nsformatter/) – Pawan Rai May 26 '14 at 13:31
  • Not offhand, no. I'd have to go hunting for examples just like you would, or read the docs and configure a number formatter myself. – Duncan C May 26 '14 at 14:36
0

After looking over http://nshipster.com/nsformatter/ and the giant NSNumberFormatter_Class doc I've come up with this code that prints everything to my requirements:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setUsesSignificantDigits: YES];
numberFormatter.maximumSignificantDigits = 100;
[numberFormatter setGroupingSeparator:@""];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
label.text = [numberFormatter stringFromNumber:stringFromNumber:@(1234.567800000555)];      

This will actually print 1234.56780000056 (missing the 12th decimal place and rounding it up to the 11th decimal place) though I'm happy enough with this.

I'm still cleaning up the answer, I don't need maximumSignificantDigits = 100 obviously, but generally having a large number there helps to ensure I'm getting all the decimal places I need.

I had to set setGroupingSeparator:@"" because NSNumberFormatterDecimalStyle puts commas in numbers and I don't want them (eg: Instead of getting 1,000 I want 1000).

edumike
  • 3,149
  • 7
  • 27
  • 33