在我的应用程序中,我使用 HTML 页面从用户那里获取数据,我使用类型为数字的文本字段,我的问题是,在将数字输入文本字段后,单击完成按钮数据来自带有逗号的文本字段 (, ) 分隔符,这个在 ios 5 中工作正常,但在 ios 6 中没有,请给出在 ios 6 中获取逗号分隔符的建议?
function addCommas(nStr)
{
nStr = removeCommas(nStr);
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
我使用上面的代码放置逗号,
最佳答案
不要尝试创建自定义方法来处理数字分隔符。 IOS 内置了一些方法,它的工作取决于用户所在的国家/地区。
你需要做这样的事情:
NSNumberFormatter *nf;
nf = [[NSNumberFormatter alloc] init];
[nf setMaximumFractionDigits:2]; // to show only two decimal places
NSNumber *num =[nf numberFromString:@"123456.789"];
NSLog([nf stringFromNumber:num]);
检查 apple's doc for NSNumberFormatter ,了解更多信息和选项。
关于javascript - 如何在 ios 6 中将逗号 (,) 分隔符作为小数点分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13486954/