3

I use this method to show keyboard with decimal separator

myTextField.keyboardType=UIKeyboardTypeDecimalPad;

How can I change comma to dot separator?

I have a Finnish locale. With comma, decimals doesn't work on my app.

-(IBAction)calculate {
    float x = ([paino.text floatValue]) / ([pituus.text floatValue]) *10000;    
    label.text = [[NSString alloc] initWithFormat:@"%0.02f", x];
}
2
  • The decimal separator depends on the current locale. On my iPhone I have a German locale and a decimal comma, on the simulator US locale and dot separator, so it seems to work. Do you have a different locale selected on your device (or simulator - you didn't say where it happens)? Commented May 11, 2011 at 7:33
  • Yes, on the simulator I have US locale and there is a dot as separator, and it works. But on the device is comma and it doesn't work. I don't know ho to use a comma as decimal separator. Commented May 11, 2011 at 9:03

2 Answers 2

10

OK so you edit a text field using a numeric keyboard, which is dependent on the current locale, and thus get a text representation of a number, which is dependendt on the current locale, too. After editing has finished you read it and want to transform into a number.

To convert you would use NSNumberFormatter like this:

NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];

You can setup the formatter as you will, setting locale (!), limits, formatting, decimal/grouping separator, number of decimal digits etc. Then you just use it:

float number = [nf numberFromString: field.text];

And that's all! Now you have the number even if the text includes comma, provided you let both: keyboard and formatter, to have the same format, same style - i.e. probably just let current locale be used all over the place.

EDIT

this is a currency formatter, that can convert between string and number for currencies:

NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
[nf setNumberStyle: NSNumberFormatterCurrencyStyle];
[nf setRoundingMode: NSNumberFormatterRoundHalfUp];
[nf setMaximumFractionDigits: 2]

this is a percentage formatter with 4 decimal places:

NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
[nf setNumberStyle: NSNumberFormatterPercentStyle];
[nf setRoundingMode: NSNumberFormatterRoundHalfUp];
[nf setMaximumFractionDigits: 4];

both in the current locale.

As you see you can define the style, digits, rounding behaviour and much more, depending on numbers you are trying to enter. For more details (it is really a lot you can do with the NSNumberFormatter) you should read Apple docs, it would go beyond the scope of SO answer to describe it all.

In your case, provided that paino and pituus are also UITextFields:

-(IBAction)calculate {
    NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
    [nf setRoundingMode: NSNumberFormatterRoundHalfUp];
    [nf setMaximumFractionDigits: 2];

    float npaino = [[nf numberFromString: paino.text] floatValue];
    float npituus = [[nf numberFromString: pituus.text] floatValue];

    float x = npaino] / npituus *10000;    
    label.text = [nf stringFromNumber: [NSNumber numberWithFloat: x]];

    [nf release];
}

Now to avoid creating the formatter in each calculation you could make it an instance variable, since you need only one for those conversions.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Tomasz, but I am new to IOS SDK and Xcode, can you provide some more information. I add some piece of my code, it is a simple calc function, trying learn from book.
Thank you Tomasz for a lot of information, but I have some errors. Can you check a screenshot - db.tt/gGOFNeT
yea, right, my error, to work with NSNumberFormatter you have to use NSNumber instances and not float values directly- look at the edited code. Now I don't have XCode here to make a test, so bear with me and look at the docs in case of error - if you use XCode 4 you can show a help view within the right inspector panel, clicking on the links there will take you to the doc page in XCode organizer. iOS API is so huge, I'm still doing it all the time.
2

Easyer that way:
[[yourField text] stringByReplacingOccurrencesOfString:@"," withString:@"."]
It will work in all ways and languages.

In your code, it will be:

-(IBAction)calculate {
    float fPaino = [[paino.text stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue];
    float x = fPaino / ([pituus.text floatValue]) *10000;    
    label.text = [[NSString alloc] initWithFormat:@"%0.02f", x];
}

Something else: are you sure to need an "alloc" for the result? As the label.text contains already its retain/release, you can simply make a [NSString stringWithFormat:@"%0.02f", x]

1 Comment

Trying to retroactively modify my apps wouldn't be cost effective but this is a good enough workaround, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.