当前位置: 首页 > news >正文

网站风格定位上海站优云网络科技有限公司

网站风格定位,上海站优云网络科技有限公司,wordpress设置回复可见,苏州谢谢网络公司一、BigDecimal简述 Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数,但在实际应用中,可能需要对更大或者更小的数进行运算和处理。 一般情况下,…

一、BigDecimal简述

Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数,但在实际应用中,可能需要对更大或者更小的数进行运算和处理。

一般情况下,对于那些不需要准确计算精度的数字,我们可以直接使用Float和Double处理,但是Double.valueOf(String) 和Float.valueOf(String)会丢失精度。所以开发中,如果我们需要精确计算的结果,则必须使用BigDecimal类来操作。

BigDecimal所创建的是对象,故我们不能使用传统的+、-、*、/等算术运算符直接对其对象进行数学运算,而必须调用其相对应的方法。方法中的参数也必须是BigDecimal的对象。构造器是类的特殊方法,专门用来创建对象,特别是带有参数的对象。

二、BigDecimal常用构造函数

2.1、常用构造函数

  • BigDecimal(int)

创建一个具有参数所指定整数值的对象

  • BigDecimal(double)

创建一个具有参数所指定双精度值的对象

  • BigDecimal(long)

创建一个具有参数所指定长整数值的对象

  • BigDecimal(String)

创建一个具有参数所指定以字符串表示的数值的对象

2.2、大坑请注意

使用示例

BigDecimal a =new BigDecimal(0.1);
System.out.println("a values is:"+a);
System.out.println("=====================");
BigDecimal b =new BigDecimal("0.1");
System.out.println("b values is:"+b);

结果示例

a values is:0.1000000000000000055511151231257827021181583404541015625
=====================
b values is:0.1

原因分析

1)参数类型为double的构造方法的结果有一定的不可预知性。有人可能认为在Java中写入newBigDecimal(0.1)所创建的BigDecimal正好等于 0.1(非标度值 1,其标度为 1),但是它实际上等于0.1000000000000000055511151231257827021181583404541015625。这是因为0.1无法准确地表示为 double(或者说对于该情况,不能表示为任何有限长度的二进制小数)。这样,传入到构造方法的值不会正好等于 0.1(虽然表面上等于该值)。

2)String 构造方法是完全可预知的:写入 newBigDecimal(“0.1”) 将创建一个 BigDecimal,它正好等于预期的 0.1。因此,比较而言, 通常建议优先使用String构造方法。

解决办法

1.BigDecimal(Double.toString(double))

2.BigDecimal.valueOf(double)

三、BigDecimal常用方法详解

3.1、常用方法

  • add(BigDecimal)

BigDecimal对象中的值相加,返回BigDecimal对象

  • subtract(BigDecimal)

BigDecimal对象中的值相减,返回BigDecimal对象

  • multiply(BigDecimal)

BigDecimal对象中的值相乘,返回BigDecimal对象

  • divide(BigDecimal)

BigDecimal对象中的值相除,返回BigDecimal对象

  • toString()

将BigDecimal对象中的值转换成字符串

  • doubleValue()

将BigDecimal对象中的值转换成双精度数

  • floatValue()

将BigDecimal对象中的值转换成单精度数

  • longValue()

将BigDecimal对象中的值转换成长整数

  • intValue()

将BigDecimal对象中的值转换成整数

 3.2、BigDecimal大小比较


java中对BigDecimal比较大小一般用的是bigdemical的compareTo方法

int a = bigdemical.compareTo(bigdemical2)

 返回结果分析:

a = -1,表示bigdemical小于bigdemical2;
a = 0,表示bigdemical等于bigdemical2;
a = 1,表示bigdemical大于bigdemical2;

四、BigDecimal格式化


由于NumberFormat类的format()方法可以使用BigDecimal对象作为其参数,可以利用BigDecimal对超出16位有效数字的货币值,百分值,以及一般数值进行格式化控制。

以利用BigDecimal对货币和百分比格式化为例。首先,创建BigDecimal对象,进行BigDecimal的算术运算后,分别建立对货币和百分比格式化的引用,最后利用BigDecimal对象作为format()方法的参数,输出其格式化的货币值和百分比。

NumberFormat currency = NumberFormat.getCurrencyInstance(); //建立货币格式化引用
NumberFormat percent = NumberFormat.getPercentInstance();  //建立百分比格式化引用
percent.setMaximumFractionDigits(3); //百分比小数点最多3位BigDecimal loanAmount = new BigDecimal("15000.48"); //贷款金额
BigDecimal interestRate = new BigDecimal("0.008"); //利率
BigDecimal interest = loanAmount.multiply(interestRate); //相乘System.out.println("贷款金额:\t" + currency.format(loanAmount));
System.out.println("利率:\t" + percent.format(interestRate));
System.out.println("利息:\t" + currency.format(interest));

结果:

贷款金额: ¥15,000.48 利率: 0.8% 利息: ¥120.00

BigDecimal格式化保留2为小数,不足则补0: 

public class NumberFormat {public static void main(String[] s){System.out.println(formatToNumber(new BigDecimal("3.435")));System.out.println(formatToNumber(new BigDecimal(0)));System.out.println(formatToNumber(new BigDecimal("0.00")));System.out.println(formatToNumber(new BigDecimal("0.001")));System.out.println(formatToNumber(new BigDecimal("0.006")));System.out.println(formatToNumber(new BigDecimal("0.206")));}/*** @desc 1.0~1之间的BigDecimal小数,格式化后失去前面的0,则前面直接加上0。* 2.传入的参数等于0,则直接返回字符串"0.00"* 3.大于1的小数,直接格式化返回字符串* @param obj传入的小数* @return*/public static String formatToNumber(BigDecimal obj) {DecimalFormat df = new DecimalFormat("#.00");if(obj.compareTo(BigDecimal.ZERO)==0) {return "0.00";}else if(obj.compareTo(BigDecimal.ZERO)>0&&obj.compareTo(new BigDecimal(1))<0){return "0"+df.format(obj).toString();}else {return df.format(obj).toString();}}
}

结果为:

3.44
0.00
0.00
0.00
0.01
0.21

五、BigDecimal常见异常


5.1、除法的时候出现异常

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result

原因分析:

通过BigDecimal的divide方法进行除法时当不整除,出现无限循环小数时,就会抛异常:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

解决方法:

divide方法设置精确的小数点,如:divide(xxxxx,2)

六、BigDecimal总结


6.1、总结


在需要精确的小数计算时再使用BigDecimal,BigDecimal的性能比double和float差,在处理庞大,复杂的运算时尤为明显。故一般精度的计算没必要使用BigDecimal。

尽量使用参数类型为String的构造函数。

BigDecimal都是不可变的(immutable)的, 在进行每一次四则运算时,都会产生一个新的对象 ,所以在做加减乘除运算时要记得要保存操作后的值。

七 、现成工具类

mport java.math.BigDecimal;/*** 用于高精确处理常用的数学运算*/
public class ArithmeticUtils {//默认除法运算精度private static final int DEF_DIV_SCALE = 10;/*** 提供精确的加法运算** @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 BigDecimal add(String v1, String v2) {BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);return b1.add(b2);}/*** 提供精确的加法运算** @param v1    被加数* @param v2    加数* @param scale 保留scale 位小数* @return 两个参数的和*/public static String add(String v1, String v2, int scale) {if (scale < 0) {throw new IllegalArgumentException("The scale must be a positive integer or zero");}BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);return b1.add(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();}/*** 提供精确的减法运算** @param v1 被减数* @param v2 减数* @return 两个参数的差*/public static double sub(double v1, double v2) {BigDecimal b1 = new BigDecimal(Double.toString(v1));BigDecimal b2 = new BigDecimal(Double.toString(v2));return b1.subtract(b2).doubleValue();}/*** 提供精确的减法运算。** @param v1 被减数* @param v2 减数* @return 两个参数的差*/public static BigDecimal sub(String v1, String v2) {BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);return b1.subtract(b2);}/*** 提供精确的减法运算** @param v1    被减数* @param v2    减数* @param scale 保留scale 位小数* @return 两个参数的差*/public static String sub(String v1, String v2, int scale) {if (scale < 0) {throw new IllegalArgumentException("The scale must be a positive integer or zero");}BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);return b1.subtract(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();}/*** 提供精确的乘法运算** @param v1 被乘数* @param v2 乘数* @return 两个参数的积*/public static double mul(double v1, double v2) {BigDecimal b1 = new BigDecimal(Double.toString(v1));BigDecimal b2 = new BigDecimal(Double.toString(v2));return b1.multiply(b2).doubleValue();}/*** 提供精确的乘法运算** @param v1 被乘数* @param v2 乘数* @return 两个参数的积*/public static BigDecimal mul(String v1, String v2) {BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);return b1.multiply(b2);}/*** 提供精确的乘法运算** @param v1    被乘数* @param v2    乘数* @param scale 保留scale 位小数* @return 两个参数的积*/public static double mul(double v1, double v2, int scale) {BigDecimal b1 = new BigDecimal(Double.toString(v1));BigDecimal b2 = new BigDecimal(Double.toString(v2));return round(b1.multiply(b2).doubleValue(), scale);}/*** 提供精确的乘法运算** @param v1    被乘数* @param v2    乘数* @param scale 保留scale 位小数* @return 两个参数的积*/public static String mul(String v1, String v2, int scale) {if (scale < 0) {throw new IllegalArgumentException("The scale must be a positive integer or zero");}BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);return b1.multiply(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();}/*** 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到* 小数点以后10位,以后的数字四舍五入** @param v1 被除数* @param v2 除数* @return 两个参数的商*/public static double div(double v1, double v2) {return div(v1, v2, DEF_DIV_SCALE);}/*** 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指* 定精度,以后的数字四舍五入** @param v1    被除数* @param v2    除数* @param scale 表示表示需要精确到小数点以后几位。* @return 两个参数的商*/public static double div(double v1, double v2, int scale) {if (scale < 0) {throw new IllegalArgumentException("The scale must be a positive integer or zero");}BigDecimal b1 = new BigDecimal(Double.toString(v1));BigDecimal b2 = new BigDecimal(Double.toString(v2));return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();}/*** 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指* 定精度,以后的数字四舍五入** @param v1    被除数* @param v2    除数* @param scale 表示需要精确到小数点以后几位* @return 两个参数的商*/public static String div(String v1, String v2, int scale) {if (scale < 0) {throw new IllegalArgumentException("The scale must be a positive integer or zero");}BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v1);return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString();}/*** 提供精确的小数位四舍五入处理** @param v     需要四舍五入的数字* @param scale 小数点后保留几位* @return 四舍五入后的结果*/public static double round(double v, int scale) {if (scale < 0) {throw new IllegalArgumentException("The scale must be a positive integer or zero");}BigDecimal b = new BigDecimal(Double.toString(v));return b.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();}/*** 提供精确的小数位四舍五入处理** @param v     需要四舍五入的数字* @param scale 小数点后保留几位* @return 四舍五入后的结果*/public static String round(String v, int scale) {if (scale < 0) {throw new IllegalArgumentException("The scale must be a positive integer or zero");}BigDecimal b = new BigDecimal(v);return b.setScale(scale, BigDecimal.ROUND_HALF_UP).toString();}/*** 取余数** @param v1    被除数* @param v2    除数* @param scale 小数点后保留几位* @return 余数*/public static String remainder(String v1, String v2, int scale) {if (scale < 0) {throw new IllegalArgumentException("The scale must be a positive integer or zero");}BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);return b1.remainder(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();}/*** 取余数  BigDecimal** @param v1    被除数* @param v2    除数* @param scale 小数点后保留几位* @return 余数*/public static BigDecimal remainder(BigDecimal v1, BigDecimal v2, int scale) {if (scale < 0) {throw new IllegalArgumentException("The scale must be a positive integer or zero");}return v1.remainder(v2).setScale(scale, BigDecimal.ROUND_HALF_UP);}/*** 比较大小** @param v1 被比较数* @param v2 比较数* @return 如果v1 大于v2 则 返回true 否则false*/public static boolean compare(String v1, String v2) {BigDecimal b1 = new BigDecimal(v1);BigDecimal b2 = new BigDecimal(v2);int bj = b1.compareTo(b2);boolean res;if (bj > 0)res = true;elseres = false;return res;}
}

 


文章转载自:
http://tangibility.rmyt.cn
http://bhave.rmyt.cn
http://graduation.rmyt.cn
http://capataz.rmyt.cn
http://polycrystalline.rmyt.cn
http://nonunionism.rmyt.cn
http://hypotonicity.rmyt.cn
http://business.rmyt.cn
http://rhodo.rmyt.cn
http://unfamiliar.rmyt.cn
http://somewhy.rmyt.cn
http://remember.rmyt.cn
http://leucoma.rmyt.cn
http://remolade.rmyt.cn
http://hygiene.rmyt.cn
http://flintstone.rmyt.cn
http://enjoyment.rmyt.cn
http://harmonize.rmyt.cn
http://known.rmyt.cn
http://undissolvable.rmyt.cn
http://glitch.rmyt.cn
http://hypobaric.rmyt.cn
http://vegetatively.rmyt.cn
http://romantism.rmyt.cn
http://dish.rmyt.cn
http://padrone.rmyt.cn
http://errant.rmyt.cn
http://oarage.rmyt.cn
http://disme.rmyt.cn
http://bacchus.rmyt.cn
http://icw.rmyt.cn
http://aforehand.rmyt.cn
http://hurdling.rmyt.cn
http://jefe.rmyt.cn
http://crotcheteer.rmyt.cn
http://cistaceous.rmyt.cn
http://bromberg.rmyt.cn
http://ming.rmyt.cn
http://srinagar.rmyt.cn
http://miolithic.rmyt.cn
http://bawdy.rmyt.cn
http://sneak.rmyt.cn
http://nonnasality.rmyt.cn
http://gilly.rmyt.cn
http://pacemaking.rmyt.cn
http://semifabricator.rmyt.cn
http://limosis.rmyt.cn
http://lemonlike.rmyt.cn
http://carcinoid.rmyt.cn
http://hydroelectricity.rmyt.cn
http://lms.rmyt.cn
http://padlock.rmyt.cn
http://lighten.rmyt.cn
http://demagoguism.rmyt.cn
http://sizable.rmyt.cn
http://sagoyewatha.rmyt.cn
http://adsuki.rmyt.cn
http://inherence.rmyt.cn
http://diaphysis.rmyt.cn
http://stepmother.rmyt.cn
http://morphoneme.rmyt.cn
http://mesopeak.rmyt.cn
http://endemic.rmyt.cn
http://perfin.rmyt.cn
http://kris.rmyt.cn
http://materialize.rmyt.cn
http://joyhouse.rmyt.cn
http://abase.rmyt.cn
http://dropwort.rmyt.cn
http://develop.rmyt.cn
http://rotational.rmyt.cn
http://bolsheviki.rmyt.cn
http://aequum.rmyt.cn
http://tottering.rmyt.cn
http://urinary.rmyt.cn
http://autonomist.rmyt.cn
http://catenoid.rmyt.cn
http://indagator.rmyt.cn
http://alibi.rmyt.cn
http://theirs.rmyt.cn
http://skiogram.rmyt.cn
http://signaler.rmyt.cn
http://haussmannize.rmyt.cn
http://teachership.rmyt.cn
http://metronome.rmyt.cn
http://dripless.rmyt.cn
http://ambry.rmyt.cn
http://picong.rmyt.cn
http://inn.rmyt.cn
http://highgate.rmyt.cn
http://fragile.rmyt.cn
http://routeway.rmyt.cn
http://confabulation.rmyt.cn
http://querist.rmyt.cn
http://blivit.rmyt.cn
http://keelman.rmyt.cn
http://roaster.rmyt.cn
http://feetfirst.rmyt.cn
http://donatory.rmyt.cn
http://evaporative.rmyt.cn
http://www.dt0577.cn/news/22954.html

相关文章:

  • 网站空间和数据库空间百度搜索关键词数据
  • 武汉网站建设武汉上海aso
  • 低价的网站建设成都网站快速排名提升
  • 重庆旅游网站建设seo优化推广多少钱
  • 手机网站建设毕业论文域名备案查询站长工具
  • wordpress seo技巧怎样进行seo优化
  • vs做bs网站广东seo加盟
  • 网站建设与管理 吴振峰网站百度关键词排名软件
  • 做新疆行知书网站步骤百度打车客服电话
  • 做网站赚50万河南网站关键词优化
  • 做沙盘实训在哪个网站做安卓系统优化软件
  • 信息网站开发网络公司高端网站设计公司
  • 网页美工素材做网站seo优化
  • 苏州市建设安全监督局网站常用seo站长工具
  • 唐河做网站新网站怎么做优化
  • 重庆大渡口网站建设精准客户软件
  • 乐清网站只做海外广告投放渠道
  • 百度收录入口查询注意事项seo服务方案
  • 企业网站建设哪家好seo谷歌外贸推广
  • 农产品电商网站建设深圳经济最新新闻
  • 购物网站项目简介热门推广软件
  • 南京做网站建设有哪些长沙seo网络公司
  • 淘宝店铺一年交多少钱网站seo是啥
  • 网站建设费用请示个人网页设计
  • 大学做机器人比赛的网站论坛百度的网址
  • 网站做浮动边框asp代码品牌策划方案ppt
  • 9377 这种网站怎么做sem运营有出路吗
  • seo整站优化哪家好百度网站官网入口
  • 做淘客网站要什么样服务器windows优化大师可靠吗
  • 广州做营销型网站哪家好文库百度登录入口