繁体   English   中英

如何在ios 6中给逗号(,)分隔符作为小数分隔符?

[英]how to give comma(,) separator as decimal separator in ios 6?

在我的应用程序即时通过使用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内置了方法,它的工作原理取决于用户Country。

你需要做这样的事情:

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的NSNumberFormatter文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM