views:

288

answers:

3

For example, I have an NSDecimal myDecimal. Lets say it represents something like "-1234567.89"

How can I get a clean string representation of that decimal without any beautification / formatting? No localization? Floating point symbol = . and rest only numbers from 0 to 9, and eventually an - if it is negative? I need that string in strict technical manner. A number like 123456789876554432123456789.2231 should therefore not look like nice formatted "123,456,789,876,554,432,123,456,789.2231". You get the point right? I don't want any formatting. I'm trying all day now to get that right but everything I find always has to do with formatting. So how'd you guys do that?

+2  A: 

For easy (localized) control use an NSNumberFormatter.

Joshua Nozzi
+4  A: 

I don't believe there is such a thing as a "clean" string representation independent of specifying the locale. As many Europeans would point out, 123.45 should be written as 123,45 (using , instead of . for the decimal location). NSDecimalString() takes, as a second parameter a locale specification. If some locale uses the format you desire, pass that locale as the second parameter (see the Internationalization Guide for more info on locales).

Alternatively, you can use an NSNumberFormatter, which will give you more controll over the string representation.

Barry Wark
+2  A: 
float number = 12.345;
NSString* numberString = [NSString stringWithFormat:@"%f", number];

That will give you consistent formatting regardless of the user's current locale.

Darren
In his case, he's asking about an NSDecimal, not a standard floating point type. However, he could extract the floating point value from the NSDecimal (by creating a temporary NSDecimalNumber and using its floatValue or doubleValue methods) and then use the format string you describe.
Brad Larson