How is it that Java's BigDecimal can be this painful?
Double d = 13.3D;
BigDecimal bd1 = new BigDecimal(d);
BigDecimal bd2 = new BigDecimal(String.valueOf(d));
System.out.println("RESULT 1: "+bd1.toString());
System.out.println("RESULT 2: "+bd2.toString());
RESULT 1: 13.300000000000000710542735760100185871124267578125
RESULT 2: 13.3
Is there any ... 
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);

I found BigDecimal have setScale method, but doesn't have getScale method. How to getScale on BigDecimal?

For more info, see my other question, here. So anyway, I'm using BigDecimals to represent currency in my application (it deducts a markdown percentage and adds sales tax), but it ... 
Which data type is apt to represent a decimal number like "10364055.81".
If tried using double:
double d = 10364055.81;
But when I try to print the number, its displaying as " 1.036405581E7", which I ... 
I want to do some simple sums with some currency values expressed in BigDecimal type.
BigDecimal test = new BigDecimal(0);
System.out.println(test);
test.add(new BigDecimal(30));
System.out.println(test);
test.add(new BigDecimal(45));
System.out.println(test);
Obviously I do not understand well the BigDecimal arithmetics, see ... 
iam having problem with assigning one big decimal value to another
so i am trying such as creating one temp big decimal and add 0 to another big decimal
BigDecimal temp = new ...

I'm doing my first attempts to use BigDecimal. It seems tricky.i am running into an issue and i would like to understand what is causing it.
public static String nominator(String nbrPeople)
{
...

I need to generate random BigDecimal value from given range. How to do it in Java?


How to set thousands separator in Java?
I have String representation of BigDecimal, i want to set thousands separator and return String.

I need a financial type which will always keep numbers in X...X.YY format, even 20 digits, so I will get rid of rounding, formatting and other headache. Is there one?
Added: In ... 
I am trying to implement the dojo DataGrid component.
I have a BigDecimal[][] object that I would like to use as the store.
I am just not sure what ... 
I am using java big decimal as follwing:
class test bigDecimal{
private BigDecimal decimalResult;
public boolean iterate(String value) {
if (value == null) ...

In a web application ,I want to model an ItemForSale which has a price field.I read elsewhere that float or double should not be used for currency fields due to rounding ... 
For below java Code it dispaly 0E10 as output
BigDecimal bd = new BigDecimal("00.0000000000");
System.out.println(bd); // output is 0E10
0E10 is due to range value of Bigdecimal. but is there any way ... 
BigDecimal bd= new BigDecimal("00.0000000000");
//now bd format to 0E10
if(BigDecimal.ZERO.equals(bd)  bd.equals("0E10"))
{
flag=true;
}
There are two problems in the above code
 why variable bd automatically format to 0E10
 if condition results false value, ie it ...


I want to round a Java BigDecimal to a certain number of significant digits (NOT decimal places) eg to 4 digits:
12.3456 => 12.35
123.456 => 123.5
123456 => 123500
etc. The basic problem is ... 
I'm trying force BigDecimal to use symbols (Ex. Instead of Decimal.multiply, do Decimal *), because of the mass parenthesis being involved in this problem.
Can you guys tell me if there's a ... 
I get the BigDecimal value from a DB. It equals '0E10'. When it is passed to a freemarker, the value is wrapped as 0.
I have already tried to swith on an ... 
Perhaps you wanted to use BigDecimal.ROUND_HALF_UP instead? for ROUND_HALF_UP 1>16.41 2>16.43 3>16.43 4>16.45 5>16.45 6>16.46 7>16.48 8>16.48 9>16.5 For Above input i tested in all cases 


Hi, I have a requirement, where I need to check the BigDecimal value in a particular range. Eg: val>=15,001.01 and val<=25,001.01 set newval=5.000 Could you any one suggest me how to do this? I tried using the following approach but the result is not as expected. if(var.doubleValue()>=15001.01&&var.doubleValue()<25001.01) newvar=new BigDecimal(5.00); I am not sure if compareTo can help me use the above ... 

My code <> works fine in jdk 1.4.2_03 on windows machine and 1.5.0_06 on solaris but it fails in jdk 1.5.0_07 on solaris. <> Caused by: java.lang.NoSuchMethodError: java.math.BigDecimal.(I)V It may work if it change it to [private BigDecimal addPercentageClosing =new BigDecimal((Double)0);] But I simply want to know is it because of Version? is this not ... 
Hi Piet, and everyone. I agree with you Piet, and long before this post I has already changed it to that. That wasn't my question however. I'm trying to understand how on earth on these 3 different machines, it worked on 2 of them, and the other one not. They are all 3 32 bit operating systems and cpu's (that's the ... 



//method to allow the contents of balance to change to store a different value, if required public void setBalance (double newBalance) { //change double newBalance to BigDecimal when working with finances BigDecimal newAmount = new BigDecimal (newBalance ); //set the number of decimal places to two and specify that fractions half and greater will be rounded up newAmount = newAmount.setScale(2, BigDecimal.ROUND_HALF_UP); ... 

hi all, here is a problem while adding big decimal numbers.can anyone help me pls. double d1 = 999999999999.11; double d2 = 9.11; double d4 = (d1 + d2); System.out.println(" Adding big double numbers:::"+d4); // this will give me o/p as :: 1.00000000000822E12 but i am interested in getting the exact value. pls help me. thanks & regards, Mahesh 
As its name suggests it is for big decimals, that is values that exceed the primitive floating point ranges. Not sure about the sql question. You should use it if you need to manipulate floating point values larger then then the primitive values or the mathematical result might exceed it. I remember reading somewhere, probably Dr. Dobbs Journal, that it shouldn't ... 






Hi All, Need to know your thoughts on the same. For a new engagement we plan to discontinue usage of primitives as a programming practice and strictly follow the usage of BigDecimal instead. Just want to know from you all if there are any perils we may encounter in this process. I believe normal/advanced mathematical calculations can be handled with BigDecimal. ... 
I would say a variation of the first one. But don't create a new BigDecimal representation of 1 each time. Use the static BigDecimal ONE from the BigDecimal class (added in Java 1.5). Also, don't use the > 1.00 since that is confusing. The compareTo method returns one of three distinct values. Use them. if (aValTxAmt.compareTo(BigDecimal.ONE) == 1) { System.out.println("aValTxAmt is ... 

HI All, I have the below class definition: public class cal { BigDecimal total = new BigDecimal("100"); BigDecimal percent1 = new BigDecimal("0.9"); BigDecimal percent2 = new BigDecimal(0.90); BigDecimal percent3 = new BigDecimal(0.90f); public static void main(String[] args){ cal c = new cal(); System.out.println(c.total.multiply(c.percent1)); System.out.println(c.total.multiply(c.percent2)); System.out.println(c.total.multiply(c.percent3)); } Ouput: 90.0 90.00000000000000222044604925031308084726333618164062500 89.99999761581420898437500 I am not able to understand why we have 2 different ... 
hi.. what i want, the field in the entity and model class are of bigdecimal types: i want to do some calcultion in bigdecimal and finally store the value after using the Math.ceil() method. iam not able to covert or type the bigdecimal into some double values first and then trying to store into database. for e.g; bigdecimal requesteQty; requestedQty = ... 

Hi all, I don't know if this is the right forum to post my question. If it is not, then move to the right place. I have three conditions. I have three fields of type BigDecimal. 1. If I enter $100.00 in one field, then it should show $100.00 in the second field and $75 in the third field which is ... 
In my code I am creating a BigDecimal by passing it a string as part of the contructor : BigDecimal mynumber = new BigDecimal("12345.67"); That works fine except when the string is not using the local regional settings as me and I try and do something like : BigDecimal mynumber = new BigDecimal("12.345,67"); So what would be the clever way of ... 


I'm trying to make a program that will add the sum of an infinite geometric series(1 + 1/2 + 1/4 + 1/8....) for (count; count<= limit; count++) a = ((1)/(math.pow(2,count)); b += a; This is easy to do when "b" and "a" are doubles, but they do not provide a high enough precision, I want to use BigDecimal to many decimal ... 

I am trying to write a program using the BigDecimal class, but I have a very small understanding of their functioning. There's nothing too special about my program... Using this formula: e = (1 / 0!) + (1 / 1!) + (1 / 2!) + ... I essentially want to display e for every computation (1/n!) until e has a scale ... 
This is a two part question. Firstly is there a max limit that BigDecimal can display or is it just a case that I'm running out of memory? I need to calculate a number that has a little under 2 million digits in it and at the moment I can only seem to get an outbut for around Math.pwr(2, 1500). Regardless ... 
Hi all, I would like to point out a real limitation in Java related to floating point calculation which for such a rich language is really unfortunate We cannot use doubles or floats if we want exact calculations, say in a financial app, because of underlying binary floating point representation, so we should use BigDecimals right? But as you know its ... 


What I am trying to do is make a GUI/applet that has textfields that numbers can be entered into and when the number is entered and ENTER is pressed the textfield is updated and other textfields that are in the GUI/applet that are linked to this particular textfield will update according to a mathematical formula that links the textfields together. thanks ... 
Hi there, I have Core Java2 put out by Sun and I just finished reading up on the use of the BigDecimal class. I think I want to implement it in my code b/c I'm losing accuracy when I'm multiplying doubles but the text isn't very clear and I don't fully understand what the BigDecimal class will do. Does it just ... 



Hi all, I'm running into trouble using BigDecimal so I think my understanding of how to use it is flawed. I'm saving a BigDecimal as a Money type in SQL Server 2005, because one of our apps deals with very large sums of money. However, sometimes I get the error: Error converting data type nvarchar to decimal It seems that when ... 

I need it to be "as fast as possible". Well, I work on an unimportant private project (fractal generator) and the usual double comes quite fast to its limits. As I zoom deeper into the fractals, I need higher precision. So, I don't need a faster CPU, I'm just looking for better ways to make my code faster (with the required ... 
A form (on a Java web application) contains a BigDecimal field. With a zero initialization, it works fine in the most of situations. When a user leave the field blank, an exception will occur. It either needs to be converted into zero or ask the user to reenter a figure. I, however, can't see how to detect the blank field. Any ... 

I'm not sure what ALL functionality you may be after but it seems to me that you're reinventing the wheel here. If you know how, Math has a function for getting the square root of a number. It works with primatives so realistically everything you would be doing would be wrapping it to use the objective container that you want. PS ... 
The OP should have looked at the documentation for the constructor he used: 1. The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because ... 
Hi All , I am having trouble with a double number . When I enter a number with 15 digits before decimal point and 4 digits after the decimal, it rounds off the digit after decimal. E.g: if I enter a number say 123456789012345.1234, it gets converted to 123456789012345.1200. I tried same by using BigDecimal but it didn't work. I awnt ... 
Maybe the question is why does the Java output differ from what you expect, rather than why is the Java output incorrect? After all, floating point arithmatic inherently produces innacurate results. I "think" the point of BigDecimal is that it allows you to clearly specify in a contract the rules for rounding. The counterparty and you agree on the rules, and ... 
public BigDecimal toDegrees(BigDecimal number){ BigDecimal pi = new BigDecimal(StrictMath.PI); BigDecimal degrees = number.divide(new BigDecimal("180").multiply(pi), BigDecimal.ROUND_HALF_UP); return degrees; } Im not sure if this is a mathimatical problem or a programming problem because when using a calculator this equation is dead easy but in java Im finding it difficult to do for some reason.... Im getting answers that are very small such ... 
72. BigDecimal forums.oracle.comHi all, I have a requirement like to convert a string or double into BigDecimal and I did by constructing a new decimal String eventValue = event.getNewValue().toString(); or double eventValue = Double.parseDouble(event.getNewValue().toString()); LOG.debug("EVENT VALUE"+eventValue); BigDecimal value = new BigDecimal(eventValue); LOG.debug("Decimal value:"+value); and if I have 93 in my event value the BigDecimal is setting to 93 but I want it to ... 

I am converting BigDecimal into 2 doubles. This approach demands reversing the order of digits in the fraction no to lose leading zeroes(for example, for the value 5.0078). But this requires calculations or casting to Strings and working with String values. I'm seeking for a better and faster solution because this conversion is performed too many times. 
75. BigDecimal forums.oracle.comI have a question on the BigDecimal mathematics. I want to store the value of the square root of 8 in a BigDecimal, but I searched in the APIdocs and I couldn't find the method to do that. I know you can first calculate the square root of 8 in a double and than convert it too a BigDecimal, but the ... 
76. BigDecimal forums.oracle.com 
Hi tschodt thanks for the tip but your solution is if i want to handle 0.333... ? for my case, i need a way to control for this case that make sure that 0.3333... is converted to 0.33 instead of 0.34 and use the code shared for our friend Edited by: DR_POMPEII on Jan 13, 2008 9:58 AM 
Hi I know this topic has been discussed to death, but I still don't quite understand something. I have a method that uses doubles to do a complex calculation. Using doubles, the calculation works perfectly, even when the numbers get huge. I am replacing the doubles with BigDecimals, and the calculations seem to work until I start getting to huge numbers ... 
79. BigDecimal forums.oracle.com 
Hello everyone I'm having a problems with my POS application when I write the following code priceTextField.setText(currentEntry.getItemPrice() ); sizeTextField.setText(currentEntry.getItemSize() ); it shows an error "setText(java.lang.String) in javax.swing.Text.JTextComponent cannot be applied to (java.math.BigDecimal) .......... Accessing Static Field " I think the problem is with " .setText( ....) .......but I don't know with what to replace it p.s I'm using Netbeans 6.0 
81. BigDecimal forums.oracle.comI think it's disgraceful that Sun provide this language for us, and a huge set of libraries, and don't even bother to provide comprehensive documentation for every class in the API, in a nice easytoread HTML format. If only they did that, then you could look up BigDecimal in there and see that it has several methods for emitting a string. ... 
I'm guessing the "that's not right" was in answer to my post??? If so, you lost me. You state that 1,2034500000000334449 is preferable to 1.2034500000000334449300, but in your original post (and indeed the format you use) you stated that you wanted 1.2034500000000334449300. Sorry, not in your orig post, my mistake. You want to loose the 3 at the end. 
Light travels at 299,792,458 metres per second which is 186,282.4 miles per second. So in one (Julian) year it travels 9,460,730,472,580,800 metres. The commas in those numbers are purely for convenience, dividing the number into easily read groups of three. But the dot has a different significance. It says how big the number is  anything to the right of the ... 
When using the BigDecimal class, you should always use the String constructor. Otherwise, you will still end up with rounding errors. But you can't use arbitrary precision in this case: how would 4/3 be represented as a BigDecimal? I'm pretty sure a double can calculate Pi more than accurate enough for your purpose. 
With or without commas is fine, but I'm just not sure how to get it to display as either 20,000,000 or 20000000 and not 2.0E+7. I was blindly guessing I would have to use BigDecimal to do this. I read up on BigDecimal a little but it seems to be a little more technical and I still am not sure BigDecimal ... 

87. BigDecimal forums.oracle.com 
D5530001_VO internalBF = new D5530001_VO(); internalBF.setPlantCode((String)vo.getPlantCode()); internalBF.setFormulaCode((String)vo.getFormulaCode()); internalBF.setMnBatchWeight(vo.getBatchWeight()); As you can see from the above codes and screen print of the display fields value, I do not have any problem to get the String fields PlantCode 5001 and FormulaCode 171710 You also can see that vo.BatchWeight has value of 2000, for some reason internalBF.mnBatch 