bigdecimal

BigDecimal加减乘除

落花浮王杯 提交于 2019-11-27 04:01:00
java.math.BigDecimal。BigDecimal一共有4个够造方法,让我先来看看其中的两种用法: 第一种:BigDecimal(double val) Translates a double into a BigDecimal. 第二种:BigDecimal(String val) Translates the String repre sentation of a BigDecimal into a BigDecimal. 使用BigDecimal要用String来够造,要做一个加法运算,需要先将两个浮点数转为String,然后够造成BigDecimal,在其中一个上调用add方法,传入另一个作为参数,然后把运算的结果(BigDecimal)再转换为浮点数。 public static double add(double v1,double v2) public static double sub(double v1,double v2) public static double mul(double v1,double v2) public static double div(double v1,double v2) public static double div(double v1,double v2,int scale) public static

BigDecimal类的加减乘除

99封情书 提交于 2019-11-27 04:00:44
BigDecimal类型(+ - * /)所用的属性 11.10 BigDecimal类 对于不需要任何准确计算精度的数字可以直接使用float或double,但是如果需要精确计算的结果,则必须使用BigDecimal类,而且使用BigDecimal类也可以进行大数的操作。BigDecimal类的常用方法如表11-15所示。 表11-15 BigDecimal类的常用方法 序号 方 法 类型 描 述 1 public BigDecimal(double val) 构造 将double表示形式转换 为BigDecimal 2 public BigDecimal(int val) 构造 将int表示形式转换为 BigDecimal 3 public BigDecimal(String val) 构造 将字符串表示 形式转换为BigDecimal 4 public BigDecimal add(BigDecimal augend) 普通 加法 5 public BigDecimal subtract(BigDecimal subtrahend) 普通 减法 6 public BigDecimal multiply(BigDecimal multiplicand) 普通 乘法 7 public BigDecimal divide(BigDecimal divisor) 普通 除法 范例

java.math.BigDecimal记录

那年仲夏 提交于 2019-11-27 03:59:31
1.引言 借用《Effactive Java》这本书中的话,float和double类型的主要设计目标是为了科学计算和工程计算。他们执行二进制浮点运算,这是为了在广域数值范围上提供较为精确的快速近似计算而精心设计的。然而,它们没有提供完全精确的结果,所以不应该被用于要求精确结果的场合。但是,商业计算往往要求结果精确,这时候BigDecimal就派上大用场啦。 2.BigDecimal简介 BigDecimal 由任意精度的整数非标度值 和32 位的整数标度 (scale) 组成。如果为零或正数,则标度是小数点后的位数。如果为负数,则将该数的非标度值乘以 10 的负scale 次幂。因此,BigDecimal表示的数值是(unscaledValue × 10-scale)。 3.测试代码 3.1构造函数(主要测试参数类型为double和String的两个常用构造函数) BigDecimal aDouble =new BigDecimal(1.22); System.out.println("construct with a double value: " + aDouble); BigDecimal aString = new BigDecimal("1.22"); System.out.println("construct with a String value: " +

使用BigDecimal进行精确运算

两盒软妹~` 提交于 2019-11-27 03:59:21
首先我们先来看如下代码示例: public class Test_1 { public static void main(String[] args) { System.out.println(0.06+0.01); System.out.println(1.0-0.42); System.out.println(4.015*100); System.out.println(303.1/1000); } } 运行结果如下。 0.06999999999999999 0.5800000000000001 401.49999999999994 0.30310000000000004 你认为你看错了,但结果却是是这样的。问题在哪里呢?原因在于我们的计算机是二进制的。浮点数没有办法是用二进制进行精确表示。我们的 CPU 表示浮点数由两个部分组 成 :指数和尾数,这样的表示方法一般都会失去一定的精确度,有些浮点数运算也会产生一定的误差。如: 2.4 的二进制表示并非就是精确的 2.4 。反而最为接近的二进制表示是 2.3999999999999999 。浮点数的值实际上是由一个特定的数学公式计算得到的。 其实java 的 float 只能用来进行科学计算或工程计算,在大多数的商业计算中,一般采用 java.math.BigDecimal 类来进行精确计算。 在使用 BigDecimal

Java BigDecimal详解

百般思念 提交于 2019-11-27 03:58:55
一、引言 借用《Effactive Java》这本书中的话,float和double类型的主要设计目标是为了科学计算和工程计算。他们执行二进制浮点运算,这是为了在广域数值范围上提供较为精确的快速近似计算而精心设计的。然而,它们没有提供完全精确的结果,所以不应该被用于要求精确结果的场合。但是,货币计算往往要求结果精确,这时候可以使用int、long或BigDecimal。本文主要讲述BigDecimal使用过程中的一些陷阱、建议和技巧。 二、 不可变性 BigDecimal是 不可变类 ,每一个操作(加减乘除等)都会返回一个新的对象, 下面以加法操作为例 。 BigDecimal a =new BigDecimal("1.22"); System.out.println("construct with a String value: " + a); BigDecimal b =new BigDecimal("2.22"); a.add(b); System.out.println("a plus b is : " + a); 我们很容易会认为会输出: construct with a String value: 1.22 a plus b is :3.44 但实际上 a plus b is : 1.22 下面我们就来分析一下加法操作的源码 public BigDecimal add

How can I divide properly using BigDecimal

余生长醉 提交于 2019-11-27 03:53:38
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? Rohan Grover You havent specified a scale for the result. Please try this 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,2, BigDecimal.ROUND_HALF_UP); System.out

How to display a number with always 2 decimal points using BigDecimal?

回眸只為那壹抹淺笑 提交于 2019-11-27 03:04:25
问题 I am using BigDecimal to get some price values. Requirement is something like this, what ever the value we fetch from database, the displayed valued should have 2 decimal points. Eg: fetched value is 1 - should be displayed as 1.00 fetched value is 1.7823 - should be displayed as 1.78 I am using setScale(2, BigDecimal.ROUND_HALF_UP) but still some places, if the data from DB is a whole number then the same is being displayed !! I mean if the value is 0 from DB its displayed as 0 only. I want

What to do with Java BigDecimal performance?

家住魔仙堡 提交于 2019-11-27 02:53:51
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 (where a , b and c are fixed-point values). The problem, however, lies with this (1/b) . I can't use fixed

Java BigDecimal remove decimal and trailing numbers

元气小坏坏 提交于 2019-11-27 02:37:08
问题 I'm new to Java and trying to take a BigDecimal (for example 99999999.99) and convert it to a string but without the decimal place and trailing numbers. Also, I don't want commas in the number and rounding is not needed. I've tried: Math.Truncate(number) but BigDecimal is not supported. Any ideas? Thanks very much. 回答1: Try number.toBigInteger().toString() 回答2: Use this. BigDecimal truncated= number.setScale(0,BigDecimal.ROUND_DOWN); 回答3: BigDecimal without fractions is BigInteger. Why don't

Use of java.math.MathContext

旧城冷巷雨未停 提交于 2019-11-27 02:07:13
问题 Recently I tried understanding the use of java.math.MathContext but failed to understand properly. Is it used for rounding in java.math.BigDecimal . If yes why does not it round the decimal digits but even mantissa part. From API docs, I came to know that it follows the standard specified in ANSI X3.274-1996 and ANSI X3.274-1996/AM 1-2000 specifications but I did not get them to read online. Please let me know if you have any idea on this. 回答1: @jatan Thanks for you answer. It makes sense.