在java中表示大十进制的数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1605910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:40:02  来源:igfitidea点击:

data type to represent a big decimal in java

javabigdecimaljdk1.4

提问by jai

Which data type is apt to represent a decimal number like "10364055.81".

哪种数据类型适合表示像“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.

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

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.

我应该使用 BigDecimal 吗?但它显示为10364055.81000000052154064178466796875。是否有任何数据类型可以按原样显示值?此外,该数字可能比作为示例的数字大。

BTW, will using BigDecimal effect the performance of the application?? I might use this in almost all my DTOs.

顺便说一句,使用 BigDecimal 会影响应用程序的性能吗??我可能会在几乎所有的 DTO 中使用它。

采纳答案by Andrzej Doyle

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

您应该使用 BigDecimal - 但使用 String 构造函数,例如:

new BigDecimal("10364055.81");

If you pass a doubleto BigDecimal, Java must create that double first - and since doubles cannot represent most decimal fractions accurately, it doescreate the value as 10364055.81000000052154064178466796875and thenpasses it to the BigDecimal constructor. In this case BigDecimal has no way of knowing that you actually meant the rounder version.

如果您将 a 传递double给 BigDecimal,Java 必须首先创建该双精度数 - 由于双精度数不能准确表示大多数小数,它确实会创建该值10364055.81000000052154064178466796875然后将其传递给 BigDecimal 构造函数。在这种情况下 BigDecimal 无法知道您实际上是指更圆的版本。

Generally speaking, using non-String constructors of BigDecimal should be considered a warning that you're not getting the full benefit of the class.

一般来说,使用 BigDecimal 的非 String 构造函数应该被视为警告,表明您没有获得该类的全部好处。

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.45for example.

编辑- 基于重新阅读您想要做的事情,我最初的主张可能太强了。当您需要精确表示十进制值时,BigDecimal 是一个不错的选择(货币处理是显而易见的选择,例如,您不希望 5.99 * 一百万5990016.45

But if you're not worried about the number being stored internallyas 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.formatcan do much the same thing.

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

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.

至于性能 - BigDecimals 自然会比使用原语慢。但是,通常情况下,除非您的程序的绝大多数都涉及数学运算,否则您实际上不太可能注意到任何速度差异。这并不是说你应该到处使用 BigDecimals;相反,如果您可以从它们的功能中获得真正的好处,而这些功能将很难或不可能实现doubles,那么请不要担心它们理论上引入的微小性能差异。

回答by Brian Agnew

How a number is displayed is distinctfrom how the number is stored.

如何显示的号码是不同的,从数量的存储方式。

Take a look at DecimalFormatfor controlling how you can display your numbers when a double (or float etc.).

查看DecimalFormat以控制如何在双精度(或浮点数等)时显示数字。

Note that choosing BigDecimal over double (or vice versa) has pros/cons, and will depend on your requirements. See herefor more info. From the summary:

请注意,选择 BigDecimal 而不是 double(反之亦然)有优点/缺点,这取决于您的要求。请参阅此处了解更多信息。从摘要:

In summary, if raw performance and space are the most important factors, primitive floating-point types are appropriate. If decimal values need to be represented exactly, high-precision computation is needed, or fine control of rounding is desired, only BigDecimal has the needed capabilities.

总之,如果原始性能和空间是最重要的因素,那么原始浮点类型是合适的。如果需要精确表示十进制值,需要高精度计算,或者需要精确控制舍入,只有 BigDecimal 具有所需的功能。

回答by sinuhepop

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

为了保存这个数字,双倍就足够了。如果您的问题是在打印或将其放入字符串时不喜欢该格式,则可以使用 NumberFormat:http: //java.sun.com/javase/6/docs/api/java/text/NumberFormat.html

回答by rachvela

you can use double and display if with System.out.printf().

您可以使用 double 和 display if with System.out.printf()。

double d = 100003.81;
System.out.printf("%.10f", d);

.10f - means a double with precision of 10

.10f - 表示精度为 10 的双精度值