How to convert string with number to NSDecimalNumber that has a comma not a decimal point?
Asked Answered
G

3

14

I have an interface giving me numbers like this 0000000012345,78

So i figured out how to make a number out of them. But I need to calculate with that number and what I actually need is a decimal number.

NSNumberFormatter *fmtn = [[NSNumberFormatter alloc] init];
[fmtn setFormatterBehavior:NSNumberFormatterBehavior10_4];
[fmtn setNumberStyle:NSNumberFormatterDecimalStyle];
[fmtn setDecimalSeparator:@","];
NSNumber *test = [fmtn numberFromString:@"0000000012345,78"];

How can I make my NSNumber to a NSDecimalNumber?

EDIT: This is the code I ended up with:

NSDictionary *localeDict = [NSDictionary dictionaryWithObject:@"," forKey:@"NSDecimalSeparator"];
NSDecimalNumber *test = [[NSDecimalNumber alloc] initWithString:@"00000000012345,78" locale:localeDict];

How to put together the locale dictionary could not be described as "well documented" and it took me some googling to find an example.

This one also worked:

NSLocale *deLoc = [[NSLocale alloc] initWithLocaleIdentifier:@"de"];
NSDecimalNumber *testd = [NSDecimalNumber decimalNumberWithString:@"00000000012345,78" locale:deLoc];
Ga answered 1/11, 2010 at 1:17 Comment(2)
Off the top of my head, [NSDecimalNumber numberFromString:@"123.123"] or something along those linesBartley
Could you please change the answer to which you have given "correct" status? The correct answer to the question in the title should have nothing whatsoever to do with strings.Tonguetied
J
39

To convert an NSNumber to NSDecimalNumber, wouldn't it make more sense to avoid the character representation altogether with this code?

NSNumber* source = ...;

NSDecimalNumber* result = [NSDecimalNumber decimalNumberWithDecimal:[source decimalValue]]; 
Jurado answered 10/5, 2012 at 19:49 Comment(1)
I hate to criticize my own answer, but it really does miss the point of the original question and doesn't deserve to be so highly rated nor even to be the accepted answer. The original problem is: given a string containing a comma decimal separator, (which is coming from some interface over which the person has no control) how to convert to NSDecimalNumber.Jurado
P
1
[NSDecimalNumber decimalNumberWithString:@"0000000012345,78"];

Use caution about the locale, though; if you run that code on an iPhone whose region format is not set to French, it might not return what you expect. So you might want to use:

+ (NSDecimalNumber *)decimalNumberWithString:(NSString *)numericString locale:(NSDictionary *)locale

instead.

Pelton answered 1/11, 2010 at 1:35 Comment(1)
The point is that the interface does give me explicitly a comma (,) and this is why I set hardcoded setDecimalSeparator:@",". The iPhone App is targeted for an area where the decimal separator is "." so i have to make sure it works their, too. But your example does get me an idea. I can set the locale to france (hardcoded) and then it will always work. Thanks.Ga
S
1

If you check out the NSDecimal Class Reference, you'll see you can create new NSDecimalNumbers from NSStrings (with and without a locale), actual numbers, etc.

If you wanted to convert an NSNumber to an NSDecimalNumber, you could do something like this:

NSDictionary *locale = ...;
NSNumber *number = ...;
NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithString:[number descriptionWithLocale:locale] locale:locale];

Of course, you'll have to correctly create the locale, and such, but that's an exercise left up to you (it might be handy to check out the NSNumber Class Reference, the NSLocale Class Reference, and the Locales Programming Guide).

Sleigh answered 1/11, 2010 at 1:38 Comment(1)
Thanks for your answer. As commented on the other response the problem is that I have a "," always because this is what I get from the interface. But I can hard-code a locale which uses the ",". Thanks for your answer.Ga

© 2022 - 2024 — McMap. All rights reserved.