bigdecimal

BigDecimal equals() versus compareTo()

江枫思渺然 提交于 2019-11-26 17:17:42
Consider the simple test class: import java.math.BigDecimal; /** * @author The Elite Gentleman * */ public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub BigDecimal x = new BigDecimal("1"); BigDecimal y = new BigDecimal("1.00"); System.out.println(x.equals(y)); System.out.println(x.compareTo(y) == 0 ? "true": "false"); } } You can (consciously) say that x is equal to y (not object reference), but when you run the program, the following result shows: false true Question: What's the difference between compareTo() and equals() in

Java BigDecimal memory usage?

吃可爱长大的小学妹 提交于 2019-11-26 16:58:49
问题 Is there a guideline for estimating the amount of memory consumed by a BigDecimal ? Looking for something similar to these guidelines for estimating String memory usage. 回答1: If you look at the fields in the source for BigDecimal there is: BigDecimal: long intCompact +8 bytes int precision +4 bytes int scale +4 bytes String stringCache +? BigInteger intVal +? BigInteger: int bitCount +4 bytes int bitLength +4 bytes int firstNonzeroIntNum +4 bytes int lowestSetBit +4 bytes int signum +4 bytes

BigDecimal加减乘除运算

杀马特。学长 韩版系。学妹 提交于 2019-11-26 16:51:34
Java代码 import java.math.BigDecimal; /** * 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精 * 确的浮点数运算,包括加减乘除和四舍五入。 */ public class Arith{ //默认除法运算精度 private static final int DEF_DIV_SCALE = 10 ; //这个类不能实例化 private Arith(){ } /** * 提供精确的加法运算。 * @param v1 被加数 * @param v2 加数 * @return 两个参数的和 */ public static double add( double v1, double v2){ BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.add(b2).doubleValue(); } /** * 提供精确的减法运算。 * @param v1 被减数 * @param v2 减数 * @return 两个参数的差 */ public static double sub( double v1, double v2){ BigDecimal b1 =

Java Double相加出现的怪事

江枫思渺然 提交于 2019-11-26 16:51:19
问题的提出: 编译运行下面这个程序会看到什么 [java] view plain copy public class test { public static void main(String args[]) { System.out.println(0.05 + 0.01); System.out.println(1.0 - 0.42); System.out.println(4.015 * 100); System.out.println(123.3 / 100); } }; 你没有看错!结果确实是 [java] view plain copy 0.060000000000000005 0.5800000000000001 401.49999999999994 1.2329999999999999 Java中的简单浮点数类型float和double不能够进行运算。不光是Java,在其它很多编程语言中也有这样的问题。在大多数情况下,计算的结果是准确的,但是多试几次(可以做一个循环)就可以试出类似上面的错误。现在终于理解为什么要有BCD码了。 这个问题相当严重,如果你有9.999999999999元,你的计算机是不会认为你可以购买10元的商品的。 在有的编程语言中提供了专门的货币类型来处理这种情况,但是Java没有。现在让我们看看如何解决这个问题。 解决方案

Set specific precision of a BigDecimal

自闭症网瘾萝莉.ら 提交于 2019-11-26 16:38:50
问题 I have an XSD that requires me to use a BigDecimal for a lat/lon. I currently have the lat/lon as doubles, and convert them to BigDecimal, but I am only required to use about 12 places of precision. I have not been able to figure out how to set that. Can anyone help me with this? 回答1: The title of the question asks about precision. BigDecimal distinguishes between scale and precision. Scale is the number of decimal places. You can think of precision as the number of significant figures, also

Ruby BigDecimal sanity check (floating point newb)

允我心安 提交于 2019-11-26 16:07:50
问题 Is my understanding correct that with Ruby BigDecimal types (even with varying precision and scale lengths) should calculate accurately or should I anticipate floating point shenanigans? All my values within a Rails application are BigDecimal type and I'm seeing some errors (they do have different decimal lengths), hoping it's just my methods and not my object types. 回答1: There are two common pitfalls when working with floating point arithmetic. The first problem is that Ruby floating points

java BigDecimal使用

自闭症网瘾萝莉.ら 提交于 2019-11-26 15:59:01
一、BigDecimal的加减乘除: package com.cy.test.math; import java.math.BigDecimal; public class TestBigDecimal { public static void main(String[] args) { //加法 double d1 = 1.234; double d2 = 2.341; System.out.println(d1 + d2); //3.575 System.out.println(d1 * d2); //2.8887940000000003精度出问题了 System.out.println(d1 / d2); //0.5271251601879539 这个是对的 //使用BigDecimal,加法 BigDecimal b1 = new BigDecimal(Double.toString(d1)); BigDecimal b2 = new BigDecimal(Double.toString(d2)); System.out.println(b1.add(b2).doubleValue()); //3.575 //减法 System.out.println(b1.subtract(b2).doubleValue()); //-1.107 //乘法 System.out

使用BigDecimal进行数字运算

给你一囗甜甜゛ 提交于 2019-11-26 15:55:38
计算数字时,可以使用BigDecimal进行运算。 String number = "100"; BigDecimal decimal1 = new BigDecimal(number); String number1 = "10"; BigDecimal decimal2 = new BigDecimal(number1); System.out.println("加法:"+number+"+"+number1+"="+decimal1.add(decimal2).toString()); System.out.println("减法:"+number+"-"+number1+"="+decimal1.subtract(decimal2).toString()); System.out.println("乘法:"+number+"*"+number1+"="+decimal1.multiply(decimal2).toString()); System.out.println("除法:"+number+"/"+number1+"="+decimal1.divide(decimal2).toString()); 以上是四个基本的运算,还有平均数等没有写出来。 有一点需要注意,使用 BigDecimal decimal1 = new BigDecimal(number);

Rounding BigDecimal to *always* have two decimal places

六月ゝ 毕业季﹏ 提交于 2019-11-26 15:49:26
I'm trying to round BigDecimal values up, to two decimal places. I'm using BigDecimal rounded = value.round(new MathContext(2, RoundingMode.CEILING)); logger.trace("rounded {} to {}", value, rounded); but it doesn't do what I want consistently: rounded 0.819 to 0.82 rounded 1.092 to 1.1 rounded 1.365 to 1.4 // should be 1.37 rounded 2.730 to 2.8 // should be 2.74 rounded 0.819 to 0.82 I don't care about significant digits, I just want two decimal places. How do I do this with BigDecimal? Or is there another class/library better suited to this? value = value.setScale(2, RoundingMode.CEILING) 来源

How to do a fractional power on BigDecimal in Java?

。_饼干妹妹 提交于 2019-11-26 15:28:22
In my little project I need to do something like Math.pow(7777.66, 5555.44) only with VERY big numbers. I came across a few solutions: Use double - but the numbers are too big Use BigDecimal.pow but no support for fractional Use the X^(A+B)=X^A*X^B formula (B is the remainder of the second num), but again no support for big X or big A because I still convert to double Use some kind of Taylor series algorithm or something like that - I'm not very good at math so this one is my last option if I don't find any solutions (some libraries or a formula for (A+B)^(C+D)). Anyone knows of a library or