bigdecimal

BigDecimal保留小数

三世轮回 提交于 2019-12-23 04:33:05
public class test1_format { public static void main(String[] args) { BigDecimal decimal = new BigDecimal("1.12345"); System.out.println(decimal); BigDecimal setScale = decimal.setScale(4,BigDecimal.ROUND_HALF_DOWN); System.out.println(setScale); BigDecimal setScale1 = decimal.setScale(4,BigDecimal.ROUND_HALF_UP); System.out.println(setScale1); } } 参数定义 ROUND_CEILING Rounding mode to round towards positive infinity. 向正无穷方向舍入 ROUND_DOWN Rounding mode to round towards zero. 向零方向舍入 ROUND_FLOOR Rounding mode to round towards negative infinity. 向负无穷方向舍入 ROUND_HALF_DOWN Rounding mode to round towards

BigDecimal - check value is within double range

ε祈祈猫儿з 提交于 2019-12-22 12:58:16
问题 I have a Java application which parses a number from somewhere, and checks that it is a valid int (between Integer.MIN_VALUE and Integer.MAX_VALUE) or a valid double (between Double.MIN_VALUE and Double.MAX_VALUE). I'm using this code: import java.math.BigDecimal; import java.math.BigInteger; public class Test { public static final BigDecimal DOUBLE_MAX = BigDecimal.valueOf(Double.MAX_VALUE); public static final BigDecimal DOUBLE_MIN = BigDecimal.valueOf(Double.MIN_VALUE); public static final

Java big decimal number format exception

╄→尐↘猪︶ㄣ 提交于 2019-12-22 03:48:14
问题 Why does the code below throw a java number format exception? BigDecimal d = new BigDecimal("10934,375"); 回答1: Yes, the BigDecimal class does not take any Locale into account in its constructor that takes a String , as can be read in the Javadoc of this constructor: the fraction consists of a decimal point followed by zero or more decimal digits. If you want to parse according to a different Locale , one that uses the comma as decimals separator, you need to use java.text.DecimalFormat with a

BigDecimal in 1.8 vs. 1.9

柔情痞子 提交于 2019-12-21 16:58:47
问题 When Upgrading to ruby 1.9, I have a failing test when comparing expected vs. actual values for a BigDecimal that is the result of dividing a Float. expected: '0.495E0',9(18) got: '0.4950000000 0000005E0',18(27) googling for things like "bigdecimal ruby precision" and "bigdecimal changes ruby 1.9" isn't getting me anywhere. How did BigDecimal 's behavior change in ruby 1.9? update 1 > RUBY_VERSION => "1.8.7" > 1.23.to_d => #<BigDecimal:1034630a8,'0.123E1',18(18)> > RUBY_VERSION => "1.9.3" > 1

BigDecimal Subtraction

感情迁移 提交于 2019-12-21 12:04:19
问题 I want to substract 2 double values, and I have tried the following code. double val1 = 2.0; double val2 = 1.10; System.out.println(val1 - val2); and I got the output as, 0.8999999999999999 For getting output as 0.9 I tried with BigDecimal as follows, BigDecimal val1BD = new BigDecimal(val1); BigDecimal val2BD = new BigDecimal(val2); System.out.println(val1BD.subtract(val2BD)); And I got the output as, 0.899999999999999911182158029987476766109466552734375 Then I tried with BigDecimal.valueOf(

BigDecimal precision not persisted with JPA annotations

你说的曾经没有我的故事 提交于 2019-12-21 10:34:48
问题 I am using the javax.persistence API and Hibernate to create annotations and persist entities and their attributes in an Oracle 11g Express database. I have the following attribute in an entity: @Column(precision = 12, scale = 9) private BigDecimal weightedScore; The goal is to persist a decimal value with a maximum of 12 digits and a maximum of 9 of those digits to the right of the decimal place. After calculating weightedScore , the result is 0.1234, but once I commit the entity with the

BigDecimal precision not persisted with JPA annotations

时间秒杀一切 提交于 2019-12-21 10:34:42
问题 I am using the javax.persistence API and Hibernate to create annotations and persist entities and their attributes in an Oracle 11g Express database. I have the following attribute in an entity: @Column(precision = 12, scale = 9) private BigDecimal weightedScore; The goal is to persist a decimal value with a maximum of 12 digits and a maximum of 9 of those digits to the right of the decimal place. After calculating weightedScore , the result is 0.1234, but once I commit the entity with the

BigDecimal and Money

我与影子孤独终老i 提交于 2019-12-21 04:43:08
问题 I've researched and found that when dealing with currency, the best way to go about doing calculations is by the BigDecimal class. With that in mind, I'm working on a code that converts various types of foreign currency into US currency and vice-versa (specifically, a cashregister that takes foreign currency and converts it into US money, computes the change and returns this amount to the customer in foreign currency). As of now, many of the methods use double and two of them take in int as a

Rounding necessary with BigDecimal numbers

时间秒杀一切 提交于 2019-12-21 03:08:12
问题 I want to set scale of two BigDecimal numbers a and b . as in this example : BigDecimal a = new BigDecimal("2.6E-1095"); BigDecimal b = new BigDecimal("2.7E-1105"); int i = 112, j=1; BigDecimal aa = a.setScale(i+j); BigDecimal bb = b.setScale(i+j); and when i run i have this exception: java.lang.ArithmeticException: Rounding necessary at java.math.BigDecimal.divideAndRound(BigDecimal.java:1439) at java.math.BigDecimal.setScale(BigDecimal.java:2394) at java.math.BigDecimal.setScale(BigDecimal

Java double vs BigDecimal for latitude/longitude

雨燕双飞 提交于 2019-12-20 08:59:13
问题 When storing latitudes/longitudes which are typically of the format: 44.087585 (i.e. max 2 numbers before the dot and 6dp) do I need to bother with bigdecimals? 回答1: Using double has enough precision for accurate lat/lon down to inches for 6-7 decimal places. In aviation, if decimal degrees are used, they typically go to at least 7 decimal places. In our NASA simulations, lat/lon data are doubles while all other attitude and altitude are floats. In other words, the 6th decimal place for