Java:如何计算代表金钱的double值中包含的小数位数? [英] Java : How to count the number of decimals contained in a double value that represent money?

查看:452
本文介绍了Java:如何计算代表金钱的double值中包含的小数位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于使用Java制作的更改应用程序,我在计算double值中的小数位数时遇到问题。



双值表示钱因此,我的所有操作都必须在对原始值进行操作后将12.58之类的双值存储为12.5797886。为了抵消这种影响,我选择使用String,就像这样:

  String amountString = String.valueOf(amount); 
字符串小数= amountString.substring(amountString.indexOf('。')+ 1);

因此,如果金额为12.58,我获得58而不是57978 ...



我的问题涉及大额金额,就像12849561476651.2501535这样的金额在科学记数法中转换,同时存储在双倍中。因此,金额的双倍价值将是1.284956147665125E13



不幸的是,我不能只计算点后的数字并在位置13之后取一切。在一种情况下像这样,原始小数2501535在第三个位置包含0,位置13后的结果四舍五入为25.



我找到了一些解决方案给那些人想要在没有科学记数法的情况下打印十进制值,他们使用.printf(%f,...)或类似的东西。



但是,如果有可能,如何我可以计算双值中包含的小数位数并处理存储问题和科学记数法吗?



我必须能够知道是否有超过2位小数。此外,如果你有一个非常高性能的解决方案,即使它是一个复杂或奇怪的解决方案,它会很棒。



谢谢你

解决方案

不要使用double或float存储货币,也不要存储任何需要精确的十进制数,因为double和float可能不准确。 / p>

您可以使用BigDecimal或使用int或long并自行跟踪小数点。



有关这方面的更多信息,请参阅Joshua Bloch撰写的有效Java中的项目48:避免浮动和双重,如果需要确切答案部分: http://uet.vnu.edu.vn/~chauttm/e-books/java/ Effective.Java.2nd.Edition.May.2008.3000th.Release.pdf


For a change-making application, made with Java, I am facing a problem while counting the number of decimals in a double value.

The double value represent money, so all my operations must be made in consequences that a double value like 12.58 can be stored as 12.5797886 after an operation is made on the original value. To counter this effect, I chose to use String, just like this :

String amountString = String.valueOf(amount);
String decimals = amountString.substring(amountString.indexOf('.') + 1);

So if the amount is 12.58, I obtain 58 and not 57978...

My problem is concerning big amount, just like 12849561476651.2501535 A such amount is converted in scientific notation while stored in a double. So the double value of the amount would be 1.284956147665125E13

Unfortunately I can't just count the number after the dot and take everything after the position 13. In a situation just like this one, where the original decimals 2501535 contains a 0 at the third position, the result after the position 13 is rounded to 25.

I found some solution for people who want to print a decimal value without the scientific notation, they use .printf( %f , ... ) or something similar.

But, if it is possible, how can I count the number of decimals contained in a double value and take care of the storage problem and the scientific notation?

I must be able to know if there is more than 2 decimals. In addition, if you have a pretty performant solution, even if it is a complex or a strange one, it would be great.

Thank you

解决方案

Do not use double or float to store currency, or to store any decimal numbers that need to be exact, as double and float can be inaccurate.

You can use BigDecimal or else use an int or a long and keep track of the decimal point yourself.

For more info on this, see the section "Item 48: Avoid float and double if exact answers are required" in Effective Java by Joshua Bloch: http://uet.vnu.edu.vn/~chauttm/e-books/java/Effective.Java.2nd.Edition.May.2008.3000th.Release.pdf

这篇关于Java:如何计算代表金钱的double值中包含的小数位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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