5

I have an NSDecimal and need this as technical string, i.e. with no formatting in any way. Floating point should be a "." if there is any, and minus sign should just be a "-" if there is any. besides that, there should be no formatting going on like grouping or chinese numbers.

I was looking for 2 hours through the SDK but it seems there is nothing simple to accomplish this. Are there solutions for this?

4 Answers4

8

For NSDecimal, you can use NSDecimalString with a specified locale:

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSString *decimalString =  NSDecimalString(&decimalValue, usLocale);
[usLocale release];

The U.S. locale uses a period for the decimal separator, and no thousands separator, so I believe that will get you the kind of formatting you're looking for.

As others have pointed out, for an NSDecimalNumber you can use the -descriptionWithLocale: method with the above-specified U.S. locale. This doesn't lose you any precision.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
4

NSDecimalNumber

NSLog(@"%@", [theNumber stringValue]);

NSDecimal

NSLog(@"%@", NSDecimalString(&theDecimal, nil));
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
3

NSDecimalNumber is a subclass of NSNumber which has the -stringValue method.

stringValue

Returns the receiver’s value as a human-readable string.

- (NSString *)stringValue

Return Value

The receiver’s value as a human-readable string, created by invoking descriptionWithLocale: where locale is nil.

descriptionWithLocale:

Returns a string that represents the contents of the receiver for a given locale.

- (NSString *)descriptionWithLocale:(id)aLocale

Parameters aLocale

An object containing locale information with which to format the description. Use nil if you don’t want the description formatted.

Just call [theNumber stringValue].

Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
  • That does not work. -descriptionWithLocale will destroy the value because it converts to double. double has less precision than an NSDecimal. – Another Registered User Nov 29 '09 at 14:49
  • NSDecimalNumber has it's own descriptionWithLocale method, it does not rely on NSNumber. – zaph Nov 29 '09 at 16:36
  • Why would you think that? This example shows that `NSDecimalNumber` _isn't_ converted to a double when invoking `-stringValue`: http://pastebin.com/m7d720b57 – Georg Schölly Nov 29 '09 at 16:47
  • @zaph: That's true. But `-stringValue` calls `-descriptionWithLocale` on `NSDecimalNumber`. Hence, my answer is the exact same as yours with a nicer method name. – Georg Schölly Nov 29 '09 at 16:51
0

Use decimalNumberWithString from the NSDecimalNumber class:

NSDecimalNumber *dn = [NSDecimalNumber decimalNumberWithMantissa:12345
                                                        exponent:-100
                                                      isNegative:YES];
NSDictionary *local = nil;
NSString *ds = [dn descriptionWithLocale: local];
NSLog(@"dn: %@", dn);
NSLog(@"ds: %@", ds);

dn: -0.00000000000000000000000 … 0000000000000000000000000000000000012345
ds: -0.00000000000000000000000 … 0000000000000000000000000000000000012345
Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
zaph
  • 111,848
  • 21
  • 189
  • 228