bigdecimal

Can we use double to store monetary fields and use BigDecimal for arithmetic

痞子三分冷 提交于 2019-12-07 04:44:28
问题 I know the problem with double/float, and it's recommended to use BigDecimal instead of double/float to represent monetary fields. But double/float is more effective and space-saving. Then my question is: It's acceptable to use double/float to represent monetary fields in Java class, but use BigDecimal to take care of the arithmetic (i.e. convert double/float to BigDecimal before any arithmetic) and equal-checking? The reason is to save some space. And I really see lots of projects are using

Scale() of Divide method in BigDecimal

我与影子孤独终老i 提交于 2019-12-07 02:48:55
问题 new BigDecimal("37146555.53880000").divide(new BigDecimal("1000000")).scale() This returns 10 . But according to the API, the divide method: Returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() - divisor.scale()); So in this case, 37146555.53880000's scale is 8 , and 1000000 's scale is 0 . So the result should have a scale of 8 , not 10 . What am I missing here? Thanks 回答1: The actual result is 37.1465555388 whose scale must be 10 for it to be

Trim More than two trailing Zeros in BigDecimal

[亡魂溺海] 提交于 2019-12-07 02:10:41
问题 What would be a good way to trim more than two trailing zeros for a BigDecimal So 1.2200 would print 1.22 and 1.0000 would print 1.00 Edit As well as to return 1.222200 as 1.2222 and 1.220000001 as 1.220000001 etc. So disregarding first two zeros I want to trim any incoming 0s and not trim non-zero values One way could be to multiply, then apply the built in trim trailing zeros and then divide by 100. It could be problematic with corner cases but the values in my problem are currency based

关于买家买入一件商品以后需要进行的逻辑

时光毁灭记忆、已成空白 提交于 2019-12-07 01:05:31
public OrderDTO create(OrderDTO orderDTO) { String orderId = KeyUtil.genUniqueKey(); BigDecimal orderAmount = new BigDecimal(BigInteger.ZERO); //1.查询商品(数量,价格) for (OrderDetail orderDetail : orderDTO.getOrderDetailList()) { ProductInfo productInfo = productService.findById(orderDetail.getProductId()); if (null == productInfo) { throw new SellerException(ResultEnum.PRODUCT_NOT_EXIST); } //2.计算总价 orderAmount = productInfo.getProductPrice().multiply(new BigDecimal(orderDetail.getProductCount())).add(orderAmount); //3.订单详情入库 BeanUtils.copyProperties(productInfo, orderDetail); orderDetail

How do I know if a BigDecimal failed to parse?

不打扰是莪最后的温柔 提交于 2019-12-07 00:57:33
问题 I'm importing data from a csv, I need to cast some values to BigDecimal, and raise an error if they can't be parsed.. From testing, BigDecimal("invalid number") returns a BigDecimal of 0. This would be ok, but kind of messy, except a valid value is 0... Float("invalid number") acts differently and throws an exception... My current solution is: class String def to_bd begin Float(self) rescue raise "Unable to parse: #{self}" end BigDecimal(self) end end Am I totally missing something? 回答1: in

BigDecimal notation eclipse plugin or nice external tool

落花浮王杯 提交于 2019-12-06 20:02:23
问题 I need to make a lot of operations using BigDecimal, and I found having to express Double a = b - c * d; //natural way as BigDecimal a = b.subtract(c.multiply(d))//BigDecimal way is not only ugly, but a source of mistakes and communication problems between me and business analysts. They were perfectly able to read code with Doubles, but now they can't. Of course a perfect solution will be java support for operator overloading, but since this not going to happen, I'm looking for an eclipse

hessian BigDecimal反序列化异常

会有一股神秘感。 提交于 2019-12-06 13:00:09
用hessian时BigDecimal反序列化时有的版本报上述异常如hessian4.0.7,有的版本反序列化结果是0,原因是服务端和客户端没有加序列化和反序列化配置,在服务提供方jar包里加上hessian的配置就可以了,如下: deserializers文件内容: java.math.BigDecimal=com.caucho.hessian.io.BigDecimalDeserializer java.sql.Date=com.caucho.hessian.io.SqlDateDeserializer java.util.Date=com.caucho.hessian.io.DateDeserializer serializers文件内容: java.math.BigDecimal=com.caucho.hessian.io.StringValueSerializer java.sql.Date=com.caucho.hessian.io.SqlDateSerializer java.util.Date=com.caucho.hessian.io.DateSerializer 来源: oschina 链接: https://my.oschina.net/u/959/blog/519173

Bigdecimal概述

自作多情 提交于 2019-12-06 10:53:31
一、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

BigDecimal Underflow

試著忘記壹切 提交于 2019-12-06 10:11:37
问题 I am trying to render a fractal called the "Lorenz Attractor" with Java. Because double does not work (values out of range), I decided to choose BigDecimals. After 38 iterations my code crashes, it gets me an ArithmeticException (Underflow). Heres some of the code: BigDecimal xnew = this.x.add(this.hBig.multiply(BigDecimal.TEN).multiply(this.x.add(this.y.negate()))); //This is the line that crashes BigDecimal ynew = this.y.add(this.hBig.multiply(this.x.negate().multiply(this.z)).add

How to handle rounding errors in Java's BigDecimal

僤鯓⒐⒋嵵緔 提交于 2019-12-06 05:05:05
I'm working with open source project (axil) that implements a scripting engine inside of java applications and I've hit a major stumbling block while trying to utilize BigDecimal's rounding. It seems that BigDecimal is converting my input to scientific notation and then applying my passed in precision to the coefficient of the SN representation of the number, rather than to its non-SN representation. For example: new BigDecimal("-232454.5324").round(new MathContext(2, RoundingMode.HALF_UP)).toString() produces a result of -2.3E+5 . This presents two problems for me. First, I am expecting a