# Negating a BigDecimal

Printable View

• 05-14-2011, 01:30 AM
ryanmk54
Negating a BigDecimal
I am making a calculator. I heard that the BigDecimal class was the best to use for precision. I tried negating a BigDecimal( using the negate() and multiply(-1) method ) and outputting it in a textField. For some reason the output doesn't come out negative. Could someone help me out?

xInput and yInput are the textFields while x and y are the variables.

Code:

```else if(command == "+/-"){         if(xInputSelected){                 BigDecimal bigDecimal1 = new BigDecimal(xInput.getText());                         x = bigDecimal1;                         x.negate();                         xInput.setText(x.toString()); }         else{                 BigDecimal bigDecimal2 = new BigDecimal(yInput.getText());                 y = bigDecimal2;                 y.multiply(new BigDecimal(-1));                 yInput.setText(y.toString());         }```
Thanks in advance.
• 05-14-2011, 01:40 AM
pbrockway2
When processing commands - or trying to! and finding the unexpected - use System.out.println() to see what is happening or not.

Code:

```[color=blue]System.out.println("command==" + command);[/color] //... else if(command == "+/-"){     [color=blue]System.out.println("Processing +/- command");[/color]     if(xInputSelected){```

-----------------

In this case the expression command=="+/-" is comparing two strings with ==. Don't do that because it is comparing something like "indentity" rather than "being made up of the same characters in the same order" which is our normal way of thinking about string equality.

Strings - like other classes - usually define their own equals() method to make precise what they mean by equality. Usually this method should be used for Strings and other classes. Try this:

Code:

`else if(command.equals("+/-")){`
• 05-15-2011, 03:40 AM
ryanmk54
Quote:

Originally Posted by pbrockway2
When processing commands - or trying to! and finding the unexpected - use System.out.println() to see what is happening or not.

Code:

```[color=blue]System.out.println("command==" + command);[/color] //... else if(command == "+/-"){     [color=blue]System.out.println("Processing +/- command");[/color]     if(xInputSelected){```

-----------------

In this case the expression command=="+/-" is comparing two strings with ==. Don't do that because it is comparing something like "indentity" rather than "being made up of the same characters in the same order" which is our normal way of thinking about string equality.

Strings - like other classes - usually define their own equals() method to make precise what they mean by equality. Usually this method should be used for Strings and other classes. Try this:

Code:

`else if(command.equals("+/-")){`

Thanks for the tip. I changed all my "==" to ".equals". But I still have the problem. I used the System.out.println() method like you suggested. The number is not negated. :(
• 05-15-2011, 04:05 AM
pbrockway2
multiply() is not "inplace" - that is calling y.multiply() is not supposed to multiply y by anything. Rather it creates and returns a new BigDecimal value with the expected value. (This is common behaviour: the String method toLowerCase() does something similar).

So what you have to do is assign the returned value to a variable:

Code:

```else{     BigDecimal bigDecimal2 = new BigDecimal(yInput.getText());     y = bigDecimal2;     [color=blue]y = [/color]y.multiply(new BigDecimal(-1));     // or replace the two lines above with     // y = bigDecimal2.multiply(new BigDecimal(-1)     yInput.setText(y.toString()); }```

It's a good idea to check out the API docs in cases like this: although you might have missed the way multiply() works unless you were aware of the possibility. But looking at the docs reveals there is a useful negate() method...
• 05-16-2011, 02:25 PM
Tolls
As obrockway is saying, BigDecimal and BigInteger (like String) are immutable. You cannot change the value in them, so all the methods in there (like add, subtract, multiply) return the result of that action, but have no effect on the particular object they've been called on.
• 05-16-2011, 07:23 PM
ryanmk54
Thanks for the help. These are two errors that I have ran into before. I should've recognized these.