数据类型表示java中的大小数 [英] data type to represent a big decimal in java

查看:131
本文介绍了数据类型表示java中的大小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪种数据类型易于表示十进制数,例如10364055.81。



如果尝试使用double:

  double d = 10364055.81; 

但是当我尝试打印数字时,它显示为 1.036405581E7 ,我不想要。



我应该使用BigDecimal吗?但它显示为 10364055.81000000052154064178466796875
是否有任何显示值的数据类型?另外这个数字可能比一个例子大。



BTW,将使用BigDecimal效果应用程序的性能??我几乎可以在所有的DTO中使用它。

解决方案

你应该使用BigDecimal - 但使用字符串构造函数,例如:

  new BigDecimal(10364055.81); 

如果您将 double 传递给BigDecimal, Java必须首先创建双倍 - 并且因为双精度不能准确地表示大多数小数部分,所以 创建值 10364055.81000000052154064178466796875 然后将它传递给BigDecimal构造函数。在这种情况下,BigDecimal无法知道你实际上意味着更圆的版本。



一般来说,使用BigDecimal的非String构造函数应该被视为警告你'没有得到全班的全部好处。



编辑 - 基于重读你想要做的事情,我最初的说法可能也是如此强大。当您需要准确表示十进制值时,BigDecimal是一个不错的选择(货币处理是明显的选择,例如,您不希望5.99 *一百万为 5990016.45 。 / p>

但是如果你不担心内部存储的数字与你输入的十进制文字略有不同,并且只是想要以相同的格式再次打印出来,然后像其他人所说的那样,一个 NumberFormat 的实例(在这种情况下, new DecimalFormat(## ######。##))可以很好地输出双精度,或者 String.format 可以做同样的事情。



至于性能 - BigDecimals自然会慢于使用原语。但通常,除非你的绝大多数程序涉及数学操作,否则你不太可能注意到任何速度差异。这并不是说你应该全部使用BigDecimals;但是,如果你能从他们的功能中获得真正的好处,而这些功能很难或不可能通过简单的双倍来实现,那么他们理论上不会出现微小的性能差异介绍。


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.

解决方案

You should use BigDecimal - but use the String constructor, e.g.:

new BigDecimal("10364055.81");

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 as 10364055.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.

这篇关于数据类型表示java中的大小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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