3

在我的应用程序中,我使用 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;
        }

我使用上面的代码放置逗号,

4

1 回答 1

1

不要尝试创建自定义方法来处理数字分隔符。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]);

检查苹果的 NSNumberFormatter 文档,了解更多信息和选项。

于 2012-11-26T12:59:59.930 回答