java中表示大十进制的数据类型
哪种数据类型适合表示十进制数,例如“10364055.81”。
如果尝试使用 double:
double d = 10364055.81;
但是当我尝试打印数字时,它显示为“1.036405581E7”,这是我不想要的。
我应该使用 BigDecimal 吗?但其显示为 10364055.81000000052154064178466796875。 是否有任何数据类型可以按原样显示值?而且该数字可能比示例中的数字还要大。
顺便说一句,使用 BigDecimal 会影响应用程序的性能吗?我可能会在几乎所有的 DTO 中使用它。
Which data type is apt to represent a decimal number like "10364055.81".
If tried using double:
double d = 10364055.81;
But when I try to print the number, its displaying as "1.036405581E7", which I don't want.
Should I use BigDecimal? But its displaying as 10364055.81000000052154064178466796875.
Is there any datatype that displays the values as it is? Also the number may be bigger than the one taken as example.
BTW, will using BigDecimal effect the performance of the application?? I might use this in almost all my DTOs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用 BigDecimal - 但使用 String 构造函数,例如:
如果将
double
传递给 BigDecimal,Java 必须首先创建该 double - 并且因为 double 不能表示大多数十进制分数准确地说,它确实将值创建为10364055.81000000052154064178466796875
,然后将其传递给 BigDecimal 构造函数。在这种情况下,BigDecimal 无法知道您实际上指的是舍入版本。一般来说,使用 BigDecimal 的非 String 构造函数应该被视为警告,表明您没有获得该类的全部好处。
编辑 - 根据重新阅读您想要做的事情,我最初的主张可能太强烈了。当您需要精确表示十进制值时,BigDecimal 是一个不错的选择(货币处理是显而易见的选择,例如,您不希望 5.99 * 100 万为
5990016.45
。但如果您不担心关于数字在内部存储为与您输入的十进制文字略有不同的值,并且只想以相同的格式再次打印出来,然后正如其他人所说,
NumberFormat
(在本例中,new DecimalFormat("########.##")
)可以很好地输出双精度值,或者 String.format 可以做同样的事情 。至于性能 - BigDecimals 自然会比使用原语慢,但是,除非您的程序的绝大多数涉及数学运算,否则您实际上不太可能注意到任何速度差异 超过;相反,如果您可以从它们的功能中获得真正的好处,而使用普通的双精度数很难或不可能实现这些功能,那么就不必担心它们理论上引入的微小性能差异。
You should use BigDecimal - but use the String constructor, e.g.:
If you pass a
double
to BigDecimal, Java must create that double first - and since doubles cannot represent most decimal fractions accurately, it does create the value as10364055.81000000052154064178466796875
and then passes it to the BigDecimal constructor. In this case BigDecimal has no way of knowing that you actually meant the rounder version.Generally speaking, using non-String constructors of BigDecimal should be considered a warning that you're not getting the full benefit of the class.
Edit - based on rereading exactly what you wanted to do, my initial claim is probably too strong. BigDecimal is a good choice when you need to represent decimal values exactly (money handling being the obvious choice, you don't want 5.99 * one million to be
5990016.45
for example.But if you're not worried about the number being stored internally as a very slightly different value to the decimal literal you entered, and just want to print it out again in the same format, then as others have said, an instance of
NumberFormat
(in this case,new DecimalFormat("########.##")
) will do the trick to output the double nicely, or String.format can do much the same thing.As for performance - BigDecimals will naturally be slower than using primitives. Typically, though, unless the vast majority of your program involves mathematical manipulations, you're unlikely to actually notice any speed difference. That's not to say you should use BigDecimals all over; but rather, that if you can get a real benefit from their features that would be difficult or impossible to realise with plain
doubles
, then don't sweat the miniscule performance difference they theoretically introduce.数字的显示方式与数字的存储方式不同。
查看 DecimalFormat控制双精度(或浮点数等)时如何显示数字。
请注意,选择 BigDecimal 而不是 double(反之亦然)有利有弊,具体取决于您的要求。请参阅 此处了解更多信息。从总结来看:
How a number is displayed is distinct from how the number is stored.
Take a look at DecimalFormat for controlling how you can display your numbers when a double (or float etc.).
Note that choosing BigDecimal over double (or vice versa) has pros/cons, and will depend on your requirements. See here for more info. From the summary:
为了保存这个数字,双倍就足够了。如果您的问题是您不喜欢打印或将其放入字符串时的格式,您可以使用 NumberFormat: http://java.sun.com/javase/6/docs/api/java/text/NumberFormat.html
A double would be enough in order to save this number. If your problem is you don't like the format when printing or putting it into a String, you might use NumberFormat: http://java.sun.com/javase/6/docs/api/java/text/NumberFormat.html
您可以使用 double 并通过 System.out.printf() 显示 if 。
.10f - 表示精度为 10 的双精度数
you can use double and display if with System.out.printf().
.10f - means a double with precision of 10