bigdecimal

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

邮差的信 提交于 2019-11-27 20:25:49
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.0 and 2.00? It seems likely that there must be a reason for this, since the Comparable documentation,

JAVA简单精确计算工具类

走远了吗. 提交于 2019-11-27 19:14:01
1 public class ArithUtil { 2 3 // 默认除法运算精度 4 private static final int DEF_DIV_SCALE = 10; 5 6 private ArithUtil() { 7 8 } 9 10 /** 11 * 提供精确的加法运算。 12 * 13 * @param v1 14 * 被加数 15 * @param v2 16 * 加数 17 * @return 两个参数的和 18 */ 19 public static double add(double v1, double v2) { 20 BigDecimal b1 = new BigDecimal(Double.toString(v1)); 21 BigDecimal b2 = new BigDecimal(Double.toString(v2)); 22 return b1.add(b2).doubleValue(); 23 } 24 25 public static double add(double v1, double v2,int scale) { 26 BigDecimal b1 = new BigDecimal(Double.toString(v1)).setScale(scale, BigDecimal.ROUND_HALF_DOWN); 27

Double类型的运算精度问题

六眼飞鱼酱① 提交于 2019-11-27 18:11:00
在Java中简单浮点数类型float和double不能进行运算,float和double只能用来做科学计算或者是工程计算,在商业计算中我们要用java.math.BigDecimal。对于货币精度已知的情况,可以使用整型。 问题提出 public class DoubleFormat { public static void main(String[] args) { double d1 = 1.0; double d2 = 0.58; System.out.println(d1 - d2); } } 执行结果:0.42000000000000004 使用BigDecimal对double进行运算 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 =

保留两位小数的写法

你离开我真会死。 提交于 2019-11-27 18:09:55
项目有一个需求,把34899米转化成带小数(保留两位)的公里数输出出来.目前我知道有三种方式: 1. Math.round()最后返回的是一个整形数(显然此种方法可以排除了),当然我们可以用这个方法间接实现保留两位小数的方法比如Math.round(34899/10d)/100d 2. NumberFormat的setMaximumFractionDigits和format方法. ``` //setMaximumFractionDigits表示保留的数量,注意这个保留的最后一位小数是四舍五入后产生的,而format方法返回的是一个字符串. NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(1); String str = nf.format(count); 3. BigDecimal的divide方法.当我们需要精确计算的时候,一般是选择BigDecimal这个对象的. //BigDecimal具有多个构造方,记住绝对不能使用BigDecimal带Double类型的构造方法,会导致结果不准确. BigDecimal bd = new BigDecimal("123.236"); BigDecimal aaa = bd.setScale(2, BigDecimal.ROUND_UP);

BigDecimal 的性能问题

杀马特。学长 韩版系。学妹 提交于 2019-11-27 18:09:40
我们知道浮点数存在精度丢失问题,为了精度问题,我们常常不直接使用浮点数,常常是采用BigDecimal来替换浮点数。本文主要想探索一下BigDecimal相比于double性能上可能存在的一些问题。 于是写了一个简单的测试程序如下:( 注:以下测试为了纯测试和比较! ) import java.math.BigDecimal; public class BigDecimalEfficiency { public static int REPEAT_TIMES = 1000000; public static double computeByBigDecimal(double a, double b) { BigDecimal result = BigDecimal.valueOf(0); BigDecimal decimalA = BigDecimal.valueOf(a); BigDecimal decimalB = BigDecimal.valueOf(b); for (int i = 0; i < REPEAT_TIMES; i++) { result = result.add(decimalA.multiply(decimalB)); } return result.doubleValue(); } public static double computeByDouble

Converting BigDecimal to Integer

怎甘沉沦 提交于 2019-11-27 17:45:47
I have Hibernate method which returns me a BigDecimal. I have another API method to which I need to pass that number but it accepts Integer as parameter. I cannot change return types or variable types of both methods. Now how to convert the BigDecimal into Integer and pass it to second method? Is there a way out of this? willcodejavaforfood You would call myBigDecimal.intValueExact() (or just intValue() ) and it will even throw an exception if you would lose information. That returns an int but autoboxing takes care of that. Anon Can you guarantee that the BigDecimal will never contain a value

零基础学习java------day12------

痞子三分冷 提交于 2019-11-27 16:59:56
0.数组高级 (1)选择排序     它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的起始位置。以此类推,直到全部待排序的数据元素排完 // 选择排序 /** * asc:升序 desc:降序 * @param arr 要排序的数组 * @param isAsc 是否升序 true:升序 false:降序 */ // 下面是直接交换元素,零一种方法是定义一个最小下角标,如minIndex = i public static void selectSort(int[] arr, boolean isAsc) { for(int i=0;i<arr.length-1;i++) { for(int j=i+1;j<arr.length;j++) { if(isAsc) { if(arr[j]<arr[i]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }else { if(arr[j]>arr[i]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } } View Code (2)冒泡排序    冒泡排序(Bubble Sort)

BigDecimal summary statistics

大城市里の小女人 提交于 2019-11-27 16:12:21
I have a list of BigDecimals. List<BigDecimal> amounts = new ArrayList<>() How do i get the summary statistics of the above list using Java 8 streams without losing precision of upto 3-4 decimal places of the BigDecimal? I created a BigDecimal specialization of the generic summary statistics collector of this answer , which allowed extending it to also support summing, hence also calculating an average: /** * Like {@code DoubleSummaryStatistics}, {@code IntSummaryStatistics}, and * {@code LongSummaryStatistics}, but for {@link BigDecimal}. */ public class BigDecimalSummaryStatistics implements

Using Struts2 Tags to Formatting Numbers

China☆狼群 提交于 2019-11-27 15:18:35
I want to format some numbers in our jsp pages. first i define some resources in my porperties format.number.with2Decimal={0,number,#0.00} ...... Question1: i want to know what is the ‘#’ and '0' means? 0.00,#0.00,##.00,###0.00 who can tell me the differences between them? thanks! Question2: if i define a BigDecimal type in my action BigDecimal number1; Then my page should using a format to show this value, 1.if number1=null then show -NIL- 2.if number1=0 then show -NIL- 3.if number1>0 then show 1.00,3434.98 ..... please ignore number<0 Question3: change number1 to a String, 1.if number1=null

金额工具类

人盡茶涼 提交于 2019-11-27 15:10:52
金额工具类 import java . math . BigDecimal ; import java . text . DecimalFormat ; /** * <p> * * @author coder * @since 2019-08-17 */ public class AmountUtil { public static DecimalFormat fnum = new DecimalFormat ( "##0.00000000000000000000" ) ; /** * 格式化金额 * * @param valueStr * @return String */ public static String formatMoney ( String valueStr ) { if ( valueStr == null || valueStr == "" ) { valueStr = "0.00" ; } return fnum . format ( new BigDecimal ( valueStr ) ) ; } /** * 金额相加 * * @param valueStr 基础值 * @param addStr 被加数 * @return String */ public static String moneyAdd ( String valueStr ,