UIKeyboardTypeDecimalPad - change comma to point

I use this method to display a decimal-separated keyboard

myTextField.keyboardType=UIKeyboardTypeDecimalPad;

How to change comma to dot separator?

I have Finnish. Decimal places do not work in my application.

-(IBAction)calculate {
    float x = ([paino.text floatValue]) / ([pituus.text floatValue]) *10000;    
    label.text = [[NSString alloc] initWithFormat:@"%0.02f", x];
}
+3
source share
2 answers

OK, so you are editing the text field using the numeric keypad, which depends on the current locale and thus gets a text representation of the number, which also depends on the current locale. After editing is complete, you will read it and want to convert to a number.

For conversion, you should use NSNumberFormatter as follows:

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

, , (!), , , /, .. :

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

! , , : , - , .

, :

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

4 :

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

.

, , , , , . ( NSNumberFormatter), Apple, SO-, .

, , paino pituus 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];
}

, , , .

+10

:
[[yourField text] stringByReplacingOccurrencesOfString: @ "," withString: @ "."

.

:

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

- : "alloc" ? label.text /, [NSString stringWithFormat: @ "% 0.02f", x]

+2

All Articles