bigdecimal

new BigDecimal(double) vs new BigDecimal(String) [duplicate]

对着背影说爱祢 提交于 2019-12-17 16:48:06
问题 This question already has answers here : BigDecimal from Double incorrect value? (3 answers) Convert double to BigDecimal and set BigDecimal Precision (8 answers) Closed 4 years ago . When BigDecimal is used with an input of double and BigDecimal with an input of String different results seem to appear. BigDecimal a = new BigDecimal(0.333333333); BigDecimal b = new BigDecimal(0.666666666); BigDecimal c = new BigDecimal("0.333333333"); BigDecimal d = new BigDecimal("0.666666666"); BigDecimal x

“new BigDecimal(13.3D)” results in imprecise “13.3000000000000007105..”?

孤人 提交于 2019-12-17 15:56:12
问题 How is it that Java's BigDecimal can be this painful? Double d = 13.3D; BigDecimal bd1 = new BigDecimal(d); BigDecimal bd2 = new BigDecimal(String.valueOf(d)); System.out.println("RESULT 1: "+bd1.toString()); System.out.println("RESULT 2: "+bd2.toString()); RESULT 1: 13.300000000000000710542735760100185871124267578125 RESULT 2: 13.3 Is there any situation where Result 1 would be desired? I know that Java 1.5 changed the toString() method but was this the intended consequence? Also I realise

Why is BigDecimal.equals specified to compare both value and scale individually?

隐身守侯 提交于 2019-12-17 15:53:29
问题 This is not a question about how to compare two BigDecimal objects - I know that you can use compareTo instead of equals to do that, since equals is documented as: Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method). The question is: why has the equals been specified in this seemingly counter-intuitive manner? That is, why is it important to be able to distinguish between 2

Why does new BigDecimal(“0.0”).stripTrailingZeros() have a scale of 1?

巧了我就是萌 提交于 2019-12-17 12:15:33
问题 Running this simple program: public static void main(final String... args) { System.out.println(BigDecimal.ZERO.scale()); System.out.println(new BigDecimal("0").scale()); System.out.println(new BigDecimal("0.0").stripTrailingZeros().scale()); System.out.println(new BigDecimal("1.0").stripTrailingZeros().scale()); } outputs: 0 0 1 0 My question is rather simple: why doesn't the third println output 0 ? That would seem logical... EDIT : OK, so, this is a very old bug: Bug Link and in fact, it

round BigDecimal to nearest 5 cents

送分小仙女□ 提交于 2019-12-17 09:47:38
问题 I'm trying to figure out how to round a monetary amount upwards to the nearest 5 cents. The following shows my expected results 1.03 => 1.05 1.051 => 1.10 1.05 => 1.05 1.900001 => 1.10 I need the result to be have a precision of 2 (as shown above). Update Following the advice below, the best I could do is this BigDecimal amount = new BigDecimal(990.49) // To round to the nearest .05, multiply by 20, round to the nearest integer, then divide by 20 def result = new BigDecimal(Math.ceil(amount

Raising a decimal to a power of decimal?

别来无恙 提交于 2019-12-17 09:33:11
问题 The .net framework provides in the Math class a method for powering double. But by precision requirement I need to raise a decimal to a decimal power [ Pow(decimal a, decimal b) ]. Does the framework have such a function? Does anyone know of a library with this kind of function? 回答1: To solve my problem I found some expansion series, and them I had them implemented to solve the equation X^n = e^(n * ln x). // Adjust this to modify the precision public const int ITERATIONS = 27; // power

Format a BigDecimal as String with max 2 decimal digits, removing 0 on decimal part

与世无争的帅哥 提交于 2019-12-17 08:30:39
问题 I have a BigDecimal number and i consider only 2 decimal places of it so i truncate it using: bd = bd.setScale(2, BigDecimal.ROUND_DOWN) Now I want to print it as String but removing the decimal part if it is 0, for example: 1.00 -> 1 1.50 -> 1.5 1.99 -> 1.99 I tried using a Formatter, formatter.format but i always get the 2 decimal digits. How can I do this? Maybe working on the string from bd.toPlainString()? 回答1: I used DecimalFormat for formatting the BigDecimal instead of formatting the

BigDecimal加减乘除

╄→尐↘猪︶ㄣ 提交于 2019-12-17 05:22:09
BigDecimal加减乘除计算 原创haiyinshushe 发布于2018-09-16 12:06:50 阅读数 219315 收藏 展开 前阵子做题遇到了大数的精确计算,再次认识了bigdecimal 关于Bigdecimal意外的有许多小知识点和坑,这里特此整理一下为方便以后学习,希望能帮助到其他的萌新 前阵子做题遇到了大数的精确计算,再次认识了bigdecimal 关于Bigdecimal意外的有许多小知识点和坑,这里特此整理一下为方便以后学习,希望能帮助到其他的萌新 BigDecimal的运算——加减乘除 首先是bigdecimal的初始化 这里对比了两种形式,第一种直接value写数字的值,第二种用string来表示 BigDecimal num1 = new BigDecimal(0.005); BigDecimal num2 = new BigDecimal(1000000); BigDecimal num3 = new BigDecimal(-1000000); //尽量用字符串的形式初始化 BigDecimal num12 = new BigDecimal("0.005"); BigDecimal num22 = new BigDecimal("1000000"); BigDecimal num32 = new BigDecimal("-1000000");

Java中BigDecimal的8种舍入模式

眉间皱痕 提交于 2019-12-17 01:24:51
Java中BigDecimal的8种舍入模式 java.math.BigDecimal 不可变的、任意精度的有符号十进制数。BigDecimal 由任意精度的整数非标度值和32位的整数标度(scale)组成。 如果为零或正数,则标度是小数点后的位数。如果为负数,则将该数的非标度值乘以10的负scale次幂。 因此,BigDecimal表示的数值是(unscaledValue × 10-scale)。 与之相关的还有两个类: java.math.MathContext: 该对象是封装上下文设置的不可变对象,它描述数字运算符的某些规则,如数据的精度,舍入方式等。 java.math.RoundingMode: 这是一种枚举类型,定义了很多常用的数据舍入方式。 这个类用起来还是很比较复杂的,原因在于舍入模式,数据运算规则太多太多, 不是数学专业出身的人看着中文API都难以理解,这些规则在实际中使用的时候在翻阅都来得及。 在银行、帐户、计费等领域,BigDecimal提供了精确的数值计算。其中8种舍入方式值得掌握。 1、ROUND_UP 舍入远离零的舍入模式。 在丢弃非零部分之前始终增加数字(始终对非零舍弃部分前面的数字加1)。 注意,此舍入模式始终不会减少计算值的大小。 2、ROUND_DOWN 接近零的舍入模式。 在丢弃某部分之前始终不增加数字(从不对舍弃部分前面的数字加1,即截短)。

Representing Monetary Values in Java [closed]

故事扮演 提交于 2019-12-16 22:37:09
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . I understand that BigDecimal is recommended best practice for representing monetary values in Java. What do you use? Is there a better library that you prefer to use instead? 回答1: BigDecimal all the way. I've heard of some folks creating their own Cash or Money classes which