views:

45

answers:

2

I have this block of code executed when pressing a number:

    NSString *currentValue = [@"" stringByAppendingFormat:@"%.02f", [[[[[textField text] stringByReplacingOccurrencesOfString:@"." withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""] doubleValue]/100.0f];
            //I am using this to obtain always a number with 2 decimals.

    NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];
    [f setMinimumFractionDigits:2];
    [f setMaximumFractionDigits:2];
    [f setGroupingSeparator:@" "];

    NSNumber *currentNumberValue = [f numberFromString:currentValue];
    NSLog(@"1: %@", currentValue);
    NSLog(@"2: %@", [currentNumberValue stringValue]);

Now if I run this in the simulator and press 3 I get the following results:

1: 0.03
2: 0.03

If I run it on the device I have:

1: 0.03
2: 0

So basically on the device the formated number is 0.
What I have also noticed is that on the simulator I get '.' as a decimal separator and on the device I have ','.

And because of this it never gets further. Any number I press it still remains 0.

What seems to be the problem?

A: 

Try it like this:

NSString *currentValue = [textField text];
float currentFloat = [currentValue floatValue];
NSLog(@"%.2f",currentFloat); //string representation of floatValue
NSLog(@"%@",currentValue); //string currentValue
JustinXXVII
+1  A: 

Your device is apparently set to a European (or wherever) locale that uses , as the decimal separator. Try adding this line after the line where you alloc and init your number formatter:

[f setDecimalSeparator:@"."];

Or use the setLocale method (or change the locale your device is set to).

MusiGenesis
Thank you. It worked. On the device I had comma (,) as a separator and on this way (with setDecimalSeparator) it is ok.
Parkyprg
Thank *you* - this was my first iPhone answer. :)
MusiGenesis
I was about to congratulate on that when I noticed you have a mere 28.3k reputation already ;) Congrats anyway!
Joseph Tura
Eh, that was all from C# and jokes. :)
MusiGenesis
If you could help me how to find out which is the decimal separator on the device I would appreciate it very much. Thanks.
Parkyprg
@Parkyprg: I don't think you can explicitly set just the decimal separator on an iPhone. Go to Settings | General | International and change the Region Format to "United States", and your device should be back to using the "normal" decimal separator.
MusiGenesis
@MusiGenesis: I don't want to set it, I want to get it. Right now I used a NSNumberFormatter and extract a substring from it.
Parkyprg
@Parkyprg: just use the `decimalSeparator` method, which will return `.` or `,` or whatever the separator is. By convention in Cocoa, "get" methods omit the "get" part at the beginning of the method name, so whereas you might be looking for a "getDecimalSeparator" method, the actual method is just "decimalSeparator".
MusiGenesis