bigdecimal

BigDecimal, precision and scale

佐手、 提交于 2019-11-28 04:49:59
I'm using BigDecimal for my numbers in my application, for example, with JPA. I did a bit of researching about the terms 'precision' and 'scale' but I don't understand what are they exactly. Can anyone explain me the meaning of 'precision' and 'scale' for a BigDecimal value? @Column(precision = 11, scale = 2) Thanks! Austin D A BigDecimal is defined by two values: an arbitrary precision integer and a 32-bit integer scale . The value of the BigDecimal is defined to be . Precision: The precision is the number of digits in the unscaled value. For instance, for the number 123.45, the precision

Generating random BigDecimal value from given range

China☆狼群 提交于 2019-11-28 03:45:10
问题 I need to generate random BigDecimal value from given range. How to do it in Java? 回答1: class BigDecRand { public static void main(String[] args) { String range = args[0]; BigDecimal max = new BigDecimal(range + ".0"); BigDecimal randFromDouble = new BigDecimal(Math.random()); BigDecimal actualRandomDec = randFromDouble.divide(max,BigDecimal.ROUND_DOWN); BigInteger actualRandom = actualRandomDec.toBigInteger(); } } 回答2: I do this that way public static BigDecimal

Hibernate - BigDecimal column mapping for Custom Dialect

元气小坏坏 提交于 2019-11-28 03:37:30
问题 I'm using Hibernate as our Object-Relational Mapping, with a custom dialect for an obscure database. The Entity I'm retrieving from this database has a column thus: @Column(name = "GROSS_WEIGHT", precision = 9, scale = 3) private BigDecimal grossWeight; The database has this column defined as numeric with a precision of 9 and a scale of 3. I can see the SQL that Hibernate generates to retrieve the data, and when I perform the same Query using the database query tool it returns '9.68' for the

How to check if BigDecimal variable == 0 in java?

纵饮孤独 提交于 2019-11-28 02:59:46
I have the following code in Java; BigDecimal price; // assigned elsewhere if (price.compareTo(new BigDecimal("0.00")) == 0) { return true; } What is the best way to write the if condition? Use compareTo(BigDecimal.ZERO) instead of equals() : if (price.compareTo(BigDecimal.ZERO) == 0) // see below Comparing with the BigDecimal constant BigDecimal.ZERO avoids having to construct a new BigDecimal(0) every execution. FYI, BigDecimal also has constants BigDecimal.ONE and BigDecimal.TEN for your convenience. Note! The reason you can't use BigDecimal#equals() is that it takes scale into

Best method to round up to the nearest 0.05 in java

落花浮王杯 提交于 2019-11-28 02:03:35
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 rounding requirment can be achieved using the logic : (float) Math.ceil(taxAmount / 0.05f) * 0.05f; Adding

BigDecimal to the power of BigDecimal on Java/Android

£可爱£侵袭症+ 提交于 2019-11-28 01:54:49
I have a simple BigDecimal that I want to power to another BigDecimal, for example 11.11^-1.54. What would be the neatest way to do it in Java/Android? I don't like the idea of converting to doubles because it is for a medical application, so I would appreciate the biggest presicion possible. So far I have reviewed http://commons.apache.org/math/ and math stuff from Google Guava, but found nothing. Edit: The whole calculation is complex, and has many operations like this. I need as much precision at the end as possible. A utility class for BigDecimal https://github.com/tareknaj/BigFunctions

Advice welcomed on creating my own Swing component

五迷三道 提交于 2019-11-28 01:40:18
Recently I asked which was the best Swing component to bind to a BigDecimal variable (with some particular editing properties). It turns out that none of the standard Swing components suit me completely, nor did the third-party Swing component libraries I've found out there. So I’ve decided to create my own Swing component. Component description: I want to extend JTextField or JFormattedTextField , so my new component can be easily bound to a BigDecimal variable. The component will have customizable scale and length properties. Behavior: When the component is drawn, it shows only the decimal

generating pi to nth digit java

倾然丶 夕夏残阳落幕 提交于 2019-11-28 00:25:46
问题 I wanted to know how I can generate pi to the nth digit. I have a couple of basic ideas. Use Math.PI and increase the precision (if that's possible) Use Euler's formula to generate pi but even here, I would need to increase the precision (I think) There is also Srinivasa Ramanujan's formula for generating PI which is known for it's rapid convergence. This formula seems difficult to implement. I believe, I would have to also increase deicmal precision here. So in short, either way, I would

浮点型数据精确计算

﹥>﹥吖頭↗ 提交于 2019-11-28 00:15:37
** * 参考:https://www.jianshu.com/p/c3c6cf0dce66 * @author zls * @date 2019/8/20 */ public class ComputedDemo { public static void main(String[] args) { // JAVA的double型数据不能进行精确计算 double n = 1.01 + 2.13; System.out.println("n = " + n); // n = 3.1399999999999997 // double型数值在整数部分超过7位时就自动转化为科学记数法表示。 System.out.println(12345678.0); // 1.2345678E7 /** * 浮点型数据运算: * BigDecimal是用来精确计算的 * 缺点:使用BigDecimal的坏处是性能比double和float差,在处理庞大,复杂的运算时尤为明显,因根据实际需求决定使用哪种类型。 */ BigDecimal x = new BigDecimal("2"); // BigDecimal x = BigDecimal.valueOf(2); BigDecimal y = new BigDecimal("3"); BigDecimal z = x.add(y); System

BigDecimal用法详解

情到浓时终转凉″ 提交于 2019-11-27 20:56:37
一、简介 Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数。在实际应用中,需要对更大或者更小的数进行运算和处理。float和double只能用来做科学计算或者是工程计算,在商业计算中要用java.math.BigDecimal。BigDecimal所创建的是对象,我们不能使用传统的+、-、*、/等算术运算符直接对其对象进行数学运算,而必须调用其相对应的方法。方法中的参数也必须是BigDecimal的对象。构造器是类的特殊方法,专门用来创建对象,特别是带有参数的对象。 二、构造器描述 BigDecimal(int) 创建一个具有参数所指定整数值的对象。 BigDecimal(double) 创建一个具有参数所指定双精度值的对象。 BigDecimal(long) 创建一个具有参数所指定长整数值的对象。 BigDecimal(String) 创建一个具有参数所指定以字符串表示的数值的对象。 三、方法描述 add(BigDecimal) BigDecimal对象中的值相加,然后返回这个对象。 subtract(BigDecimal) BigDecimal对象中的值相减,然后返回这个对象。 multiply(BigDecimal) BigDecimal对象中的值相乘