Pavel Kaljunen
Pavel Kaljunen

Reputation: 1291

UIKeyboardTypeDecimalPad - change comma to dot

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];
}

Upvotes: 3

Views: 8610

Answers (2)

Dugh
Dugh

Reputation: 171

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]

Upvotes: 2

Tomasz Stanczak
Tomasz Stanczak

Reputation: 13164

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.

Upvotes: 10

Related Questions