How to format double to money format? Eg: 3.234,12
I have a string input by the user, this is string is treated to have only numbers, such as: "3472042"
.
I then convert this string to a double value, but I want it to be formatted as a money value, like: 3.472,042
Just like DecimalFormat in Java if that helps.
I tried some things with NSNumberFormatter but no luck. Can anyone give me a hand with that?
Used this at first:
NSString *string = txtOtherPower.text; NSCharacterSet *removeCharSet = [NSCharacterSet characterSetWithCharactersInString:@"•€£¥./:;@#$%&*(){}[]!?\\-|_=开发者_运维问答+\"`'~^abcdefghijklmnopqrstvwxyz' '"]; string = [[string componentsSeparatedByCharactersInSet:removeCharSet] componentsJoinedByString:@""];
So that the only acceptable characters are comma ","
and numbers.
The problem is if I type something like "2,2,2,2,,,,,1,,2"
then it just won't do anything.
And that messes with my string/number.
I need perfectly formatted money values... Any ideas?
Create an NSNumberFormatter
, set its style to NSNumberFormatterCurrencyStyle
or NSNumberFormatterDecimalStyle
(or, if those styles don't work for you, configure it with setPositiveFormat:
, setNegativeFormat:
etc.), and then convert the number to a string with stringFromNumber:
.
See the documentation for this class for details.
精彩评论