bigdecimal

How can I store a BigDecimal (Java) value in a Real (SQlite) column?

本小妞迷上赌 提交于 2019-12-18 04:20:37
问题 I'm still having big problems with BigDecimal (the trail of tears started here, and continued to here so far. Now I've got the opposite problem - going from BigDecimal in the POJO to REAL in the SQLite table. The POJO is defined as: public class DeliveryItem { private int _id; private String _invoiceNumber; private String _UPC_PLU; private String _vendorItemId; private int _packSize; private String _description; private BigDecimal _cost; private BigDecimal _margin; private BigDecimal

Why does Java BigDecimal return 1E+1?

白昼怎懂夜的黑 提交于 2019-12-18 03:04:32
问题 Why does this code sometimes return 1E+1 whilst for other inputs (e.g. 17) the output is not printed in scientific notation? BigDecimal bigDecimal = BigDecimal.valueOf(doubleValue).multiply(BigDecimal.valueOf(100d)).stripTrailingZeros(); System.out.println("value: " + bigDecimal); 回答1: use bigDecimal.toPlainString(): BigDecimal bigDecimal = BigDecimal.valueOf(100000.0) .multiply(BigDecimal.valueOf(100d)) .stripTrailingZeros(); System.out.println("plain : " + bigDecimal.toPlainString());

Why is BigDecimal natural ordering inconsistent with equals?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 23:30:25
问题 From the Javadoc for BigDecimal: Note: care should be exercised if BigDecimal objects are used as keys in a SortedMap or elements in a SortedSet since BigDecimal 's natural ordering is inconsistent with equals . For example, if you create a HashSet and add new BigDecimal("1.0") and new BigDecimal("1.00") to it, the set will contain two elements (because the values have different scales, so are non-equal according to equals and hashCode ), but if you do the same thing with a TreeSet , the set

How to make updating BigDecimal within ConcurrentHashMap thread safe

放肆的年华 提交于 2019-12-17 22:40:52
问题 I am making an application that takes a bunch of journal entries and calculate sum. Is below way of doing it is thread/concurrency safe when there are multiple threads calling the addToSum() method. I want to ensure that each call updates the total properly. If it is not safe, please explain what do I have to do to ensure thread safety. Do I need to synchronize the get/put or is there a better way? private ConcurrentHashMap<String, BigDecimal> sumByAccount; public void addToSum(String account

BigDecimal adding wrong value

早过忘川 提交于 2019-12-17 21:25:56
问题 I have a BigDecimal defined like this: private static final BigDecimal sd = new BigDecimal(0.7d); if i print it, i get the value: 0.6999999999999999555910790149937383830547332763671875 which causes some wrong calculations. Does anyone know a way to get the exact value of 0.7 as BigDecimal ? Change it to 0.71 would view the right result, but it shouldn't be like that 回答1: Use a String literal: private static final BigDecimal sd = new BigDecimal("0.7"); If you use a double , actually public

How do I map a BigDecimal in Hibernate so I get back the same scale I put in?

╄→尐↘猪︶ㄣ 提交于 2019-12-17 18:44:26
问题 In Java, new BigDecimal("1.0") != new BigDecimal("1.00") i.e., scale matters. This is apparently not true for Hibernate/SQL Server, however. If I set the scale on a BigDecimal to a particular value, save the BigDecimal to the database via Hibernate and then re-inflate my object, I get back a BigDecimal with a different scale. For instance, a value of 1.00 is coming back as 1.000000, I assume because we're mapping BigDecimals to a column defined as NUMERIC(19,6) . I can't just define the

java数字精确计算的解决方案---BigDecimal

我的梦境 提交于 2019-12-17 18:17:24
摘自:http://www.blogjava.net/ghyghost/archive/2008/06/16/208309.html 问题的提出: 编译运行下面这个程序会看到什么? 1 public class Test{ 2 public static void main(String args[]){ 3 System.out.println(0.05+0.01); 4 System.out.println(1.0-0.42); 5 System.out.println(4.015*100); 6 System.out.println(123.3/100); 7 } 8 }; 你没有看错!结果确实是 0.060000000000000005 0.5800000000000001 401.49999999999994 1.2329999999999999 Java中的简单浮点数类型float和double不能够进行运算 。不光是Java,在其它很多编程语言中也有这样的问题。在大多数情况下,计算的结果是准确的,但是多试几次(可以做一个循环)就可以试出类似上面的错误。现在终于理解为什么要有BCD码了。 这个问题相当严重,如果你有9.999999999999元,你的计算机是不会认为你可以购买10元的商品的。 在有的编程语言中提供了专门的货币类型来处理这种情况,但是Java没有

BigDecimal in JavaScript

旧街凉风 提交于 2019-12-17 17:45:39
问题 I'm very new to JavaScript (I come from a Java background) and I am trying to do some financial calculations with small amounts of money. My original go at this was: <script type="text/javascript"> var normBase = ("[price]").replace("$", ""); var salesBase = ("[saleprice]").replace("$", ""); var base; if (salesBase != 0) { base = salesBase; } else { base = normBase; } var per5 = (base - (base * 0.05)); var per7 = (base - (base * 0.07)); var per10 = (base - (base * 0.10)); var per15 = (base -

DOUBLE精度问题

断了今生、忘了曾经 提交于 2019-12-17 17:02:20
问题的提出: 如果我们编译运行下面这个程序会看到什么? public class Test{ public static void main(String args[]){ System.out.println(0.05+0.01); System.out.println(1.0-0.42); System.out.println(4.015*100); System.out.println(123.3/100); } }; 你没有看错!结果确实是 0.060000000000000005 0.5800000000000001 401.49999999999994 1.2329999999999999 Java中的简单浮点数类型float和double不能够进行运算。不光是Java,在其它很多编程语言中也有这样的问题。在大多数情况下,计算的结果是准确的,但是多试几次(可以做一个循环)就可以试出类似上面的错误。现在终于理解为什么要有BCD码了。 这个问题相当严重,如果你有9.999999999999元,你的计算机是不会认为你可以购买10元的商品的。 在有的编程语言中提供了专门的货币类型来处理这种情况,但是Java没有。现在让我们看看如何解决这个问题。 四舍五入 我们的第一个反应是做四舍五入。Math类中的round方法不能设置保留几位小数,我们只能象这样(保留两位): public

Best method to round up to the nearest 0.05 in java

浪尽此生 提交于 2019-12-17 16:49:37
问题 Consider that a tax of 10% is applicable on all items except food. Also, an additional tax of of 5 % is applicable on imported items. If the cost of a music CD is 12.49. The tax for the item will be 1.499. If the cost of an imported bottle of perfume is 47.50, The tax on the item will be 7.125 There is a policy in place which says that taxes on an item should be rounded off to the nearest 0.05. Therefore, 1.499 should be rounded off to 1.5 and 7.125 should be rounded of to 7.25. The above