bigdecimal

Why is the Bigdecimal(double d) construction still around?

风流意气都作罢 提交于 2019-11-28 13:19:05
I've noticed substantial pain over this constructor (even here on Stack Overflow). People use it even though the documentation clearly states: The results of this constructor can be somewhat unpredictable http://java.sun.com/javase/6/docs/api/java/math/BigDecimal.html#BigDecimal(double) I've even seen a JSR-13 being APPROVED with a recommendation stating: Existing specifications that might be deprecated: We propose deprecating the BigDecimal(double) constructor, which currently gives results that are different to the Double.toString() method. Despite all this, the constructor has not yet been

java保留两位小数

懵懂的女人 提交于 2019-11-28 11:28:15
java保留两位小数 标签: java string constructor integer j2se import 2008-10-14 18:41 165404人阅读 评论 (11) 收藏 举报 目录 (?) [+] Java 保留两位小数问题: 方式一: 四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 保留两位小数 --------------------------------------------------------------- 方式二: java.text.DecimalFormat df =new java.text.DecimalFormat("#.00"); df.format(你要格式化的数字); 例:new java.text.DecimalFormat("#.00").format(3.1415926) #.00 表示两位小数 #.0000四位小数 以此类推... 方式三: double d = 3.1415926; String result = String .format("%.2f"); %.2f %. 表示 小数点前任意位数 2

java保留两位小数

六眼飞鱼酱① 提交于 2019-11-28 11:27:58
java保留两位小数问题: 方式一: 四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 保留两位小数 --------------------------------------------------------------- 方式二: java.text.DecimalFormat df =new java.text.DecimalFormat("#.00"); df.format(你要格式化的数字); 例:new java.text.DecimalFormat("#.00").format(3.1415926) #.00 表示两位小数 #.0000四位小数 以此类推... 方式三: double d = 3.1415926; String result = String .format("%.2f"); %.2f %. 表示 小数点前任意位数 2 表示两位小数 格式后的结果为f 表示浮点型 方式四: NumberFormat ddf1=NumberFormat.getNumberInstance() ; void setMaximumFractionDigits(int

Java 保留两位小数

て烟熏妆下的殇ゞ 提交于 2019-11-28 11:27:40
java保留小数问题的方法: 方法一:四舍五入 使用java.Math.BigDecimal类    double d = 12.345;   BigDecimal bd = new BigDecimal(2,BigDecimal.ROUND_HALF_UP).doubleValue(); 方法二:使用java.text.DecimalFormat类    double d = 12.345;   DecimalFormat dFormat = new DecimalFormat(".00");   dFormat.format(d);     (".00")表示保留小数点后两位小数 方法三:使用java.lang.Math类    double d = 12.345;   Math.round(d);   Math.round(double d);//返回lang类型   Math.round(float f);//返回int类型 今天在使用该方法得到保留2为小数的数字时,得到的总是整数,代码如下: standardP = ((double)Math.round(standard * 10000))/100 + "%";//standard 为两个变量相除得到的double类型的值 后来查看API发现返回值类型为lang,果断实行强制类型转换。所以再使用此方法保留小数时请慎重。

java保留两位小数

无人久伴 提交于 2019-11-28 11:27:31
java保留两位小数问题: 方式一: 四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 保留两位小数 --------------------------------------------------------------- 方式二: java.text.DecimalFormat df =new java.text.DecimalFormat("#.00"); df.format(你要格式化的数字); 例:new java.text.DecimalFormat("#.00").format(3.1415926) #.00 表示两位小数 #.0000四位小数 以此类推... 方式三: double d = 3.1415926; String result = String .format("%.2f"); %.2f %. 表示 小数点前任意位数 2 表示两位小数 格式后的结果为f 表示浮点型 方式四: NumberFormat ddf1=NumberFormat.getNumberInstance() ; void setMaximumFractionDigits(int

Efficient BigDecimal round Up and down to two decimals

邮差的信 提交于 2019-11-28 11:09:21
In java am trying to find an efficient way to round a BigDecimal to two decimals, Up or Down based on a condition. IF condition true then: 12.390 ---> 12.39 12.391 ---> 12.40 12.395 ---> 12.40 12.399 ---> 12.40 If condition false then: 12.390 ---> 12.39 12.391 ---> 12.39 12.395 ---> 12.39 12.399 ---> 12.39 What is the most efficient way to accomplish this? public static BigDecimal round(BigDecimal d, int scale, boolean roundUp) { int mode = (roundUp) ? BigDecimal.ROUND_UP : BigDecimal.ROUND_DOWN; return d.setScale(scale, mode); } round(new BigDecimal("12.390"), 2, true); // => 12.39 round(new

java编码中注意问题的总结

試著忘記壹切 提交于 2019-11-28 10:24:32
1. java中的浮点数比较事项 1.1 若精度要求不高,比如因为传感器有误差,小于0.001的数都可以认为等于0,那么就定义epsilon = 0.001: 1 private final double epsilon = 1e-9; 2 double double_x = 0.0; 3 if(Math.abs(double_x - 0) < epsilon) 4 { 5 System.out.println("true"); 6 } 1.2 转换成字符串之后用equals方法比较 1 Double.toString(double_x).equals(Double.toString(double_y)) 注意:这种方法只适用于比较精度相同的数据,并且是只用用于比较是否相等的情况下,不能用来判断大小。 1.3 转换成Long之后用==方法比较 Sun提供了一种将Double类型转换成Longo类型的方法,即 Double.doubleToLongBits() 方法,从而可以使double按照long的方法(<, >, ==)判断是否大小和是否相等。 1 Double.doubleToLongBits(0.01) == Double.doubleToLongBits(0.01; 2 Double.doubleToLongBits(0.02) > Double

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

喜你入骨 提交于 2019-11-28 09:39:57
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 that to be displayed as 0.00 Thanks BigDecimal is immutable, any operation on it including setScale(2,

Java BigDecimal remove decimal and trailing numbers

岁酱吖の 提交于 2019-11-28 09:03:53
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. Try number.toBigInteger().toString() Use this. BigDecimal truncated= number.setScale(0,BigDecimal.ROUND_DOWN); BigDecimal without fractions is BigInteger. Why don't you just use BigInteger? Here's the most elegant way I found to resolve this: public static String

Shiro_项目_审核入库订单&&库存预警&&发送邮件

二次信任 提交于 2019-11-28 08:56:03
文章目录 1 进销存模块分类 1.1 基本模块内容 1.2 进销存核心模块 2 进销存业务逻辑 2.1 建立入库订单(组合关系、主从表) 2.1.1 仓库 2.1.2 入库订单:组合关系的一方 2.1.3 入库订单明细:组合关系的多方 2.1.4 即时库存 2.2 级联保存 2.2.1 导包 2.2.2 保存测试 2.3 审核采购入库单 3 库存预警 3.1 pom.xml添加jar文件 3.2 任务调度/定时调度jar文件 3.3 发出预警的最佳时机 3.4 java.util.Timer 3.5 使用OpenSymphony Quartz 任务调度 3.5.1 ApplicationContext-qz.xml配置 3.5.2 applicationContext.xml配置 3.5.3 代码部分 4 邮件 4.1 pom.xml添加jar文件 4.2 ApplicationContext-email.xml配置 4.3 开启smtp协议 4.4 简单邮件和复杂邮件 4.4.1 简单邮件 4.4.2 复杂邮件 ,支持附件形式(图片或者文档) 1 进销存模块分类 1.1 基本模块内容 组织机构模块:公司,部门,员工 系统模块: 角色,权限,菜单-- 使用系统之前的 这些就应该维护到系统 基础数据模块:(数据字典(下拉框) )产品,产品类型,供应商(采购),客户(销售) 1.2