UITextField的数值键:浮点值的点而不是逗号 [英] UITextField's numerical pad: dot instead of comma for float values

查看:114
本文介绍了UITextField的数值键:浮点值的点而不是逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用从数字键盘填充的textField。

I'm using a textField which is filled from a numerical pad.

麻烦的是,有很多本地区域格式(例如所有欧洲人), UITextField的数字键盘上有逗号而不是点,所以每当我写一个十进制数字时,UITextField就无法识别小数点的逗号和圆号;例如23,07变为23。

Trouble is that, with lot of local region formats (all european, for example), UITextField's numerical pad has comma instead dot, so everytime I write a decimal number, UITextField can't recognise the decimal comma and it round number; for example 23,07 become 23.

我该如何解决这个问题?
我想将textField设置为USA;可能吗?怎么样?

How can I solve this? I thought to set the textField fixed on USA; is it possible? How?

我用这个读取了这个值:

I read the value using this:

var importo = (importoPrevistoTF.text as NSString).floatValue


推荐答案

潜力 SO答案的副本,使用 NSNumberFormatter

示例Swift:

let number = NSNumberFormatter().numberFromString(numberString)
if let number = number {
    let floatValue = Float(number)
}

示例(Objective-C):

Example (Objective-C):

NSNumber *number = [[NSNumberFormatter new] numberFromString: numberString];
float floatValue = number.floatValue;

这篇关于UITextField的数值键:浮点值的点而不是逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆