bigdecimal

BigDecimal setScale and round

纵饮孤独 提交于 2019-11-26 15:09:05
问题 What is the difference between this two call? (Is there any?) // 1. new BigDecimal("3.53456").round(new MathContext(4, RoundingMode.HALF_UP)); // 2. new BigDecimal("3.53456").setScale(4, RoundingMode.HALF_UP); 回答1: One important point that is alluded to but not directly addressed is the difference between "precision" and "scale" and how they are used in the two statements. "precision" is the total number of significant digits in a number. "scale" is the number of digits to the right of the

BigDecimal from Double incorrect value?

烈酒焚心 提交于 2019-11-26 14:50:03
问题 I am trying to make a BigDecimal from a string. Don't ask me why, I just need it! This is my code: Double theDouble = new Double(".3"); System.out.println("The Double: " + theDouble.toString()); BigDecimal theBigDecimal = new BigDecimal(theDouble); System.out.println("The Big: " + theBigDecimal.toString()); This is the output I get? The Double: 0.3 The Big: 0.299999999999999988897769753748434595763683319091796875 Any ideas? 回答1: When you create a double, the value 0.3 cannot be represented

BigDecimal java加减乘除

微笑、不失礼 提交于 2019-11-26 14:29:35
不论是float 还是double都是浮点数,而计算机是二进制的,浮点数会失去一定的精确度。Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。BigDecimal所创建的是对象,我们不能使用传统的+、-、*、/等算术运算符直接对其对象进行数学运算,而必须调用其相对应的方法。方法中的参数也必须是BigDecimal的对象。构造器是类的特殊方法,专门用来创建对象,特别是带有参数的对象。 下面是个例子: http://yayihouse.com/yayishuwu/chapter/2081 来源: https://blog.csdn.net/qq_37599827/article/details/98884664

Addition for BigDecimal

自作多情 提交于 2019-11-26 14:21:07
I want to do some simple sums with some currency values expressed in BigDecimal type. BigDecimal test = new BigDecimal(0); System.out.println(test); test.add(new BigDecimal(30)); System.out.println(test); test.add(new BigDecimal(45)); System.out.println(test); Obviously I do not understand well the BigDecimal arithmetics, see output behind. Test 0 0 0 Can anyone help me out? Vincent Ramdhanie The BigDecimal is immutable so you need to do this: BigDecimal result = test.add(new BigDecimal(30)); System.out.println(result); It looks like from the Java docs here that add returns a new BigDecimal:

How can i parse a String to BigDecimal? [duplicate]

孤街浪徒 提交于 2019-11-26 13:55:08
问题 This question already has an answer here: Safe String to BigDecimal conversion 9 answers I have this String: 10,692,467,440,017.120 (it's an amount). I want to parse it to a BigDecimal. The problem is that I have tried both DecimalFormat and NumbeFormat in vain. Any help? 回答1: Try this // Create a DecimalFormat that fits your requirements DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setGroupingSeparator(','); symbols.setDecimalSeparator('.'); String pattern = "#,##0.0#";

Java BigDecimal precision problems

安稳与你 提交于 2019-11-26 13:52:31
问题 I know the following behavior is an old problem, but still I don't understand. System.out.println(0.1 + 0.1 + 0.1); Or even though I use BigDecimal System.out.println(new BigDecimal(0.1).doubleValue() + new BigDecimal(0.1).doubleValue() + new BigDecimal(0.1).doubleValue()); Why this result is: 0.30000000000000004 instead of: 0.3 ? How can I solve this? 回答1: What you actually want is new BigDecimal("0.1") .add(new BigDecimal("0.1")) .add(new BigDecimal("0.1")); The new BigDecimal(double)

The best cross platform (portable) arbitrary precision math library [closed]

若如初见. 提交于 2019-11-26 12:48:51
I'm looking for a good arbitrary precision math library in C or C++. Could you please give me some advices / suggestions? The primary requirements: It MUST handle arbitrarily big integers (my primary interest is on integers). In case that you don't know what the word arbitrarily big means, imagine something like 100000! (the factorial of 100000). The precision MUST NOT NEED to be specified during library initialization / object creation. The precision should ONLY be constrained by the available resources of the system. It SHOULD utilize the full power of the platform, and should handle "small"

How can I divide properly using BigDecimal

痞子三分冷 提交于 2019-11-26 12:42:20
问题 My code sample: import java.math.*; public class x { public static void main(String[] args) { BigDecimal a = new BigDecimal(\"1\"); BigDecimal b = new BigDecimal(\"3\"); BigDecimal c = a.divide(b, BigDecimal.ROUND_HALF_UP); System.out.println(a+\"/\"+b+\" = \"+c); } } The result is: 1/3 = 0 What am I doing wrong? 回答1: You haven't specified a scale for the result. Please try this 2019 Edit: Updated answer for JDK 13. Cause hopefully you've migrated off of JDK 1.5 by now. import java.math

What to do with Java BigDecimal performance?

狂风中的少年 提交于 2019-11-26 12:36:16
问题 I write currency trading applications for living, so I have to work with monetary values (it\'s a shame that Java still doesn\'t have decimal float type and has nothing to support arbitrary-precision monetary calculations). \"Use BigDecimal!\" — you might say. I do. But now I have some code where performance is an issue, and BigDecimal is more than 1000 times (!) slower than double primitives. The calculations are very simple: what the system does is calculating a = (1/b) * c many many times