使用NSNumberFormatter生成十进制后跟零的数字 [英] Using NSNumberFormatter to generate number with trailing zeros after decimal

查看:139
本文介绍了使用NSNumberFormatter生成十进制后跟零的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift中,您如何创建NSNumberFormatter来保留小数点后(12.000)后的零,同时生成适合当前语言环境的数字?

In Swift, how would you create an NSNumberFormatter that would preserve trailing zeros after a decimal (12.000) while also generating a number appropriate for the current locale?

我当前的代码和示例输出:

My current code and example output:

let formatter = NSNumberFormatter()
formatter.numberStyle = .DecimalStyle
formatter.locale = NSLocale.currentLocale()
formatter.maximumFractionDigits = 10

var doubleNumString = "12.0"
println(formatter.numberFromString(doubleNumString)) //in English it prints 12, want it to print 12.0

var doubleNumString = "12.000"
println(formatter.numberFromString(doubleNumString)) //prints 12, want it to print 12.000

var doubleNumString = "12.125"
println(formatter.numberFromString(doubleNumString)) //prints 12.125 as expected

var doubleNumString = "1234"
println(formatter.numberFromString(doubleNumString)) //prints 1,234 as expected

我已经对它进行了编码,如果字符串以小数点("12.")结尾,那么它将不使用此格式化程序来生成数字,而是只显示数字,然后显示小数点(但是我需要进行改进,因为某些语言从右到左阅读).

I've already coded it such that if the string ends in a decimal ("12.") then it won't use this formatter to generate the number and will instead just display the number then the decimal (but I will need to improve that because some languages read right to left).

一种解决方案是检查字符串是否包含句点,如果包含,则检查其后的所有数字是否均为0,如果是,则不要通过数字格式化程序运行它,而只能通过以下方式运行int值:然后,格式化程序会在小数点后添加/添加小数,后跟适当数量的0.

One solution would be to check if the string contains a period and if so, check if all digits that follow it are 0, and if so then don't run it through the number formatter and instead run only the int value through the formatter then append/prepend the decimal followed by the appropriate number of 0's.

是否有更好/更清洁的解决方案?

Is there a better/cleaner solution?

推荐答案

如Martin R所述,您可以将minimumFractionDigitsmaximumFractionDigits设置为相同的数字,这将强制始终显示许多小数位.要知道要显示多少,您需要在小数点后至末尾取一个子字符串并计算其元素.为了知道所有小数位数是否均为0,我创建了一个辅助方法,将该子字符串转换为数字,如果它等于0,那么您就知道它们全为0.

As mentioned by Martin R, you can set the minimumFractionDigits and maximumFractionDigits to the same number which will enforce that many fraction digits always be displayed. To know how many to display you need to take a substring after the decimal to the end and count its elements. To know whether or not all of the fraction digits are 0's, I created a helper method that converts that substring to a number and if it equals 0 then you know they were all 0's.

不幸的是,您需要根据原始字符串号使用几个不同的NSNumberFormatter将字符串转换为本地化数字.因此,如果它确实包含小数,并且其后的所有内容均为0,则您需要创建一个不同的格式化程序,将字符串转换为数字,然后将该数字转换为字符串,以便在尊重用户语言环境的情况下进行显示.否则,您可以只使用原始数字格式器.

Unfortunately you need to convert the string to a localized number using a couple different NSNumberFormatters based on the original string number. So if it does contain a decimal and everything after it is a 0 then you need to create a different formatter, convert the string to a number, then convert that number to a string in order to display it respecting the user's locale. Otherwise you can just use your original number formatter.

这篇关于使用NSNumberFormatter生成十进制后跟零的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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