How to get string representation from NSDecimal without formatting?

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

How can I get a clean string representation of this decimal without any improvement / formatting? No localization? Floating point symbol =. and leave only numbers from 0 to 9, and in the end - if they are negative? I need this line in a strictly technical way. Therefore, a number like 123456789876554432123456789.2231 should not look as pretty as "123,456,789,876,554,432,123,456,789.2231". You understood correctly? I do not want formatting. I try all day to get this right, but everything I find is always related to formatting. So how did you do this?

+4
source share
3 answers

I don’t believe that there is such a thing as a β€œclean” string representation, independent of the locale. As many Europeans say, 123.45 should be written as 123.45 (using instead of decimal places). NSDecimalString() (which can be found in Linking to Basic Functions ), the locale specification is used as the second parameter. If any language uses the desired format, pass this locale as the second parameter (for more information about locales, see the Internationalization Guide ).

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

+8
source

For simple (localized) management, use NSNumberFormatter.

+3
source
 float number = 12.345; NSString* numberString = [NSString stringWithFormat:@"%f", number]; 

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

+1
source

All Articles