bigdecimal

How to get biggest BigDecimal value

我只是一个虾纸丫 提交于 2019-11-27 02:03:12
问题 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) 回答1: Its an arbitrary precision class, it will get as large as you'd like until your computer runs out of memory. 回答2: Looking at the source BigDecimal

How to convert from float to bigDecimal in java?

流过昼夜 提交于 2019-11-27 01:45:05
问题 How to convert from float to bigDecimal in java? 回答1: 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

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

不羁岁月 提交于 2019-11-27 01:44:35
问题 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. 回答1: From the javadoc of BigDecimal equals

Removing trailing zeros from BigDecimal in Java

五迷三道 提交于 2019-11-27 01:33:29
问题 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 .

Best way to convert Locale specific String to BigDecimal

别说谁变了你拦得住时间么 提交于 2019-11-27 01:14:41
问题 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

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

无人久伴 提交于 2019-11-26 23:53:55
问题 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? 回答1: 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

Advice welcomed on creating my own Swing component

风流意气都作罢 提交于 2019-11-26 23:33:52
问题 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

Java BigDecimal trigonometric methods

北城以北 提交于 2019-11-26 23:14:23
I am developing a mathematical parser which is able to evaluate String like '5+b*sqrt(c^2)' . I am using ANTLR for the parsing and make good progress. Now I fell over the Java class BigDecimal and thought: hey, why not thinking about precision here. My problem is that the Java API does not provide trigonometric methods for BigDecimal s like java.lang.Math . Do you know if there are any good math libraries like Apache Commons out there that deal with this problem? The other questions is how to realize the power method so that I can calculate 4.9 ^ 1.4 with BigDecimal s. Is this possible? A book

Converting BigDecimal to Integer

佐手、 提交于 2019-11-26 22:37:04
问题 I have Hibernate method which returns me a BigDecimal. I have another API method to which I need to pass that number but it accepts Integer as parameter. I cannot change return types or variable types of both methods. Now how to convert the BigDecimal into Integer and pass it to second method? Is there a way out of this? 回答1: You would call myBigDecimal.intValueExact() (or just intValue()) and it will even throw an exception if you would lose information. That returns an int but autoboxing

BigDecimal - to use new or valueOf

↘锁芯ラ 提交于 2019-11-26 21:53:26
I came across two ways of getting BigDecimal object out of a double d. 1. new BigDecimal(d) 2. BigDecimal.valueOf(d) Which would be a better approach? Would valueOf create a new object? In general (not just BigDecimal), what is recommended - new or valueOf? Thanks. Those are two separate questions: "What should I use for BigDecimal ?" and "What do I do in general?" For BigDecimal : this is a bit tricky, because they don't do the same thing . BigDecimal.valueOf(double) will use the canonical String representation of the double value passed in to instantiate the BigDecimal object. In other words