bigdecimal

Set specific precision of a BigDecimal

a 夏天 提交于 2019-11-28 08:52:20
I have an XSD that requires me to use a BigDecimal for a lat/lon. I currently have the lat/lon as doubles, and convert them to BigDecimal, but I am only required to use about 12 places of precision. I have not been able to figure out how to set that. Can anyone help me with this? You can use setScale() e.g. double d = ... BigDecimal db = new BigDecimal(d).setScale(12, BigDecimal.ROUND_HALF_UP); The title of the question asks about precision. BigDecimal distinguishes between scale and precision. Scale is the number of decimal places. You can think of precision as the number of significant

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

风流意气都作罢 提交于 2019-11-28 08:49:47
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 column as the required scale as I need to store both Dollar and Yen values (for example) in the same column

Rounding mode with BigDecimal in Java

末鹿安然 提交于 2019-11-28 08:25:15
问题 The following code which uses RoundingMode.HALF_EVEN , BigDecimal value1 = new BigDecimal("4.5"); value1=value1.setScale(0, RoundingMode.HALF_EVEN); BigDecimal value2 = new BigDecimal("6.5"); value2=value2.setScale(0, RoundingMode.HALF_EVEN); System.out.println(value1+"\n"+value2); displays 4 and 6 respectively. It seems to me that it should display 5 and 7 respectively because the digit to the left of the discarded fractional part (which is 5 in this case) is odd . In this case, it performs

How to get biggest BigDecimal value

筅森魡賤 提交于 2019-11-28 08:02:21
How can I get the largest possible value of a BigDecimal variable can hold? (Preferably programmatically, but hardcoding would be ok too) EDIT OK, just realized there is no such thing since BigDecimal is arbitrary precision. So I ended up with this, which is sufficiently good for my purpose: BigDecimal my = BigDecimal.valueOf(Double.MAX_VALUE) Its an arbitrary precision class, it will get as large as you'd like until your computer runs out of memory. Andrew Looking at the source BigDecimal stores it as a BigInteger with a radix, private BigInteger intVal; private int scale; and from BigInteger

Use of java.math.MathContext

蓝咒 提交于 2019-11-28 07:56:34
Recently I tried understanding the use of java.math.MathContext but failed to understand properly. Is it used for rounding in java.math.BigDecimal . If yes why does not it round the decimal digits but even mantissa part. From API docs, I came to know that it follows the standard specified in ANSI X3.274-1996 and ANSI X3.274-1996/AM 1-2000 specifications but I did not get them to read online. Please let me know if you have any idea on this. @jatan Thanks for you answer. It makes sense. Can you please explain me MathContext in the context of BigDecimal#round method. There's nothing special about

How to convert from float to bigDecimal in java?

家住魔仙堡 提交于 2019-11-28 07:11:43
How to convert from float to bigDecimal in java? dogbane BigDecimal value = new BigDecimal(Float.toString(123.4f)); From the javadocs , the string constructor is generally the preferred way to convert a float into a BigDecimal, as it doesn't suffer from the unpredictability of the BigDecimal(double) constructor. Quote from the docs: Note: For values other float and double NaN and ±Infinity, this constructor is compatible with the values returned by Float.toString(float) and Double.toString(double). This is generally the preferred way to convert a float or double into a BigDecimal , as it doesn

Why BigDecimal(“5.50”) not equals to BigDecimal(“5.5”) and how to work around this issue?

好久不见. 提交于 2019-11-28 07:08:41
Actually, I've found possible solution //returns true new BigDecimal("5.50").doubleValue() == new BigDecimal("5.5").doubleValue() Of course, it can be improved with something like Math.abs (v1 - v2) < EPS to make the comparison more robust, but the question is whether this technique acceptable or is there a better solution? If someone knows why java designers decided to implement BigDecimal's equals in that way, it would be interesting to read. From the javadoc of BigDecimal equals public boolean equals(Object x) Compares this BigDecimal with the specified Object for equality. Unlike compareTo

Removing trailing zeros from BigDecimal in Java

时间秒杀一切 提交于 2019-11-28 06:42:08
I need to remove trailing zeros from BigDecimal along with RoundingMode.HALF_UP . For instance, Value Output 15.3456 <=> 15.35 15.999 <=> 16 //No trailing zeros. 15.99 <=> 15.99 15.0051 <=> 15.01 15.0001 <=> 15 //No trailing zeros. 15.000000<=> 15 //No trailing zeros. 15.00 <=> 15 //No trailing zeros. stripTrailingZeros() works but it returns scientific notations in situations like, new BigDecimal("600.0").setScale(2, RoundingMode.HALF_UP).stripTrailingZeros(); In this case, it returns 6E+2 . I need this in a custom converter in JSF where it might be ugly for end users. So, what is the proper

Best way to convert Locale specific String to BigDecimal

孤街醉人 提交于 2019-11-28 06:17:59
I have to convert a German locale formatted String to a BigDecimal. However, I'm struggling with the best solution. The following code shows my problem: String numberString = "2.105,88"; NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); try { Number parsed = nf.parse(numberString); BigDecimal bd1 = new BigDecimal(parsed.toString()); System.out.println(bd1.toString()); BigDecimal bd2 = new BigDecimal(parsed.doubleValue()); System.out.println(bd2); BigDecimal bd3 = new BigDecimal(numberString); System.out.println(bd3); } catch (ParseException e) { e.printStackTrace(); } The outpout of

使用executor、callable以及一个Future 计算欧拉数e

家住魔仙堡 提交于 2019-11-28 06:05:19
package test; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class CaculateE { final static int num=17; public static void main(String[] args) { // TODO Auto-generated method stub //使用线程池防止多条人物提交而创建的线程 ExecutorService executor=Executors.newFixedThreadPool(1); Callable<BigDecimal> callable=new Callable<BigDecimal>() { @Override public