如何以货币格式格式化小数?
有没有办法将小数格式化为以下形式:
100 -> "100"
100.1 -> "100.10"
如果是整数,则省略小数部分。否则采用两位小数的格式。
Is there a way to format a decimal as following:
100 -> "100"
100.1 -> "100.10"
If it is a round number, omit the decimal part. Otherwise format with two decimal places.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
我建议使用 java.text 包:
这具有特定于语言环境的额外好处。
但是,如果必须的话,请截断返回的字符串(如果它是整美元):
I'd recommend using the java.text package:
This has the added benefit of being locale specific.
But, if you must, truncate the String you get back if it's a whole dollar:
或者
显示货币输出的
注意:
Locale
构造函数已被弃用。有关其他选项,请参阅获取区域设置。因此,由于
Locale
构造函数已被弃用,我们可以使用Locale.Builder()
来构造 Locale 对象。或
输出
如果您不想使用标志,请使用此方法
or
Output
Note:
Locale
constructors have been deprecated. See Obtaining a Locale for other options.So, since
Locale
constructors are deprecated, we can useLocale.Builder()
to construct a Locale object.or
Output
If you don't want to use sign use this method
我对此表示怀疑。问题是,如果是浮点数,100 就永远不是 100,它通常是 99.9999999999 或 100.0000001 或类似的东西。
如果您确实想以这种方式格式化它,则必须定义一个 epsilon,即与整数的最大距离,如果差异较小,则使用整数格式,否则使用浮点数。
像这样的事情就可以解决问题:
I doubt it. The problem is that 100 is never 100 if it's a float, it's normally 99.9999999999 or 100.0000001 or something like that.
If you do want to format it that way, you have to define an epsilon, that is, a maximum distance from an integer number, and use integer formatting if the difference is smaller, and a float otherwise.
Something like this would do the trick:
谷歌搜索后没有找到好的解决方案,只是发布我的解决方案供其他人参考。使用 priceToString 格式化货币。
I did not find any good solution after google search, just post my solution for other to reference. use priceToString to format money.
输出:
如果要更改货币,
则输出:
output:
If you want to change the currency,
output:
这是最好的方法。
100-> “100.00”
100.1-> “100.10”
this best way to do that.
100 -> "100.00"
100.1 -> "100.10"
我正在使用这个(使用 commons-lang 中的 StringUtils):
您必须只处理区域设置和要切割的相应字符串。
I'm using this one (using StringUtils from commons-lang):
You must only take care of the locale and the corresponding String to chop.
你应该做这样的事情:
打印:
100
100,10
you should do something like this:
which prints:
100
100,10
我认为这对于打印货币来说是简单明了的:
输出:$12,345.68
这个问题的可能解决方案之一:
输出:
100
100.10
I think this is simple and clear for printing a currency:
output: $12,345.68
And one of possible solutions for the question:
Output:
100
100.10
我们通常需要执行相反的操作,如果您的 json 货币字段是浮点数,则它可能为 3.1 、 3.15 或只是 3 。
在这种情况下,您可能需要对其进行舍入以正确显示(并且能够使用掩码)稍后在输入字段上):
We will usually need to do the inverse, if your json money field is an float, it may come as 3.1 , 3.15 or just 3.
In this case you may need to round it for proper display (and to be able to use a mask on an input field later):
是的。您可以使用 java.util.formatter。您可以使用“%10.2f”等格式字符串
Yes. You can use java.util.formatter. You can use a formatting string like "%10.2f"
我知道这是一个老问题但是......
I know this is an old question but...
您可以执行类似的操作,然后传递整数,然后传递美分。
You can just do something like this and pass in the whole number and then the cents after.
我同意 @duffymo 的观点,您需要使用 java.text.NumberFormat 方法来处理此类事情。实际上,您可以在其中本地完成所有格式设置,而无需自己进行任何字符串比较:
需要指出的是:
setMaximumFractionDigits()
方法。无论您确定是否应截断小数的逻辑,都应该使用
setMaximumFractionDigits()
方法。I agree with @duffymo that you need to use the
java.text.NumberFormat
methods for this sort of things. You can actually do all the formatting natively in it without doing any String compares yourself:Couple bits to point out:
Math.round(priceAsDouble * 100) % 100
is just working around the inaccuracies of doubles/floats. Basically just checking if we round to the hundreds place (maybe this is a U.S. bias) are there remaining cents.setMaximumFractionDigits()
methodWhatever your logic for determining whether or not the decimals should get truncated,
setMaximumFractionDigits()
should get used.格式从 1000000.2 到 1 000 000,20
Format from 1000000.2 to 1 000 000,20
如果您想处理货币,则必须使用 BigDecimal 类。问题是,无法在内存中存储一些浮点数(例如,您可以存储 5.3456,但不能存储 5.3455),这可能会导致错误的计算。
有一篇很好的文章如何与 BigDecimal 和货币合作:
http ://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html
If you want work on currencies, you have to use BigDecimal class. The problem is, there's no way to store some float numbers in memory (eg. you can store 5.3456, but not 5.3455), which can effects in bad calculations.
There's an nice article how to cooperate with BigDecimal and currencies:
http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html
这篇文章确实帮助我最终得到了我想要的东西。所以我只是想在这里贡献我的代码来帮助其他人。这是我的代码和一些解释。
以下代码:
将返回:
实际的 jeroensFormat() 方法:
调用方法 jeroensFormat() 的方法 displayJeroensFormat
将有以下输出:
此代码使用您当前的货币。就我而言,这是荷兰,因此我的格式化字符串与美国的某人不同。
只需注意这些数字的最后 3 个字符即可。我的代码有一个 if 语句来检查最后 3 个字符是否等于“,00”。要在美国使用它,如果它还不起作用,您可能必须将其更改为“.00”。
This post really helped me to finally get what I want. So I just wanted to contribute my code here to help others. Here is my code with some explanation.
The following code:
Will return:
The actual jeroensFormat() method:
The method displayJeroensFormat that calls the method jeroensFormat()
Will have the following output:
This code uses your current currency. In my case that's Holland so the formatted string for me will be different than for someone in the US.
Just watch the last 3 characters of those numbers. My code has an if statement to check if the last 3 characters are equal to ",00". To use this in the US you might have to change that to ".00" if it doesn't work already.
对于想要格式化货币但不希望它基于本地的人,我们可以这样做:
For the people who wants to format the currency, but does not want it to be based on local, we can do this:
这就是我所做的,使用整数将金额表示为美分:
NumberFormat.getCurrencyInstance()
的问题是,有时您确实希望 $20 成为 $20,它只是看起来比 $20.00 更好。如果有人找到更好的方法来做到这一点,使用 NumberFormat,我会洗耳恭听。
This is what I did, using an integer to represent the amount as cents instead:
The problem with
NumberFormat.getCurrencyInstance()
is that sometimes you really want $20 to be $20, it just looks better than $20.00.If anyone finds a better way of doing this, using NumberFormat, I'm all ears.
我疯狂地编写了自己的函数:
这会将整数转换为货币格式(也可以修改小数):
I was crazy enough to write my own function:
This will convert integer to currency format (can be modified for decimals as well):
输出:
$200.00
Output:
$200.00