UIKeyboardTypeDecimalPad - 将逗号更改为点 [英] 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];
}
推荐答案
好的,所以你可以编辑一个文本字段使用数字键盘,它取决于当前的语言环境,从而得到一个数字的文本表示,该数字也依赖于当前语言环境。编辑完成后,您阅读它并想要转换为数字。
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.
要转换你会像这样使用NSNumberFormatter:
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];
这就是全部!现在你有了这个数字,即使文本中包含逗号,只要你让:keyboard和formatter都具有相同的格式,相同的样式 - 也就是说只需让当前的语言环境可以在整个地方使用。
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.
编辑
这是一种货币格式化程序,可以在货币的字符串和数字之间进行转换:
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]
这是一个带有4位小数的百分比格式化程序:
this is a percentage formatter with 4 decimal places:
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
[nf setNumberStyle: NSNumberFormatterPercentStyle];
[nf setRoundingMode: NSNumberFormatterRoundHalfUp];
[nf setMaximumFractionDigits: 4];
当前区域设置。
如您所见,您可以根据您尝试输入的数字来定义样式,数字,舍入行为等等。有关更多细节(你可以用NSNumberFormatter做很多事情)你应该阅读Apple文档,它将超出SO答案的范围来描述它。
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.
在你的情况下,如果paino和pituus也是UITextFields:
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.
这篇关于UIKeyboardTypeDecimalPad - 将逗号更改为点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!