Velocity Reviews > Java > Bigdecimal problem

# Bigdecimal problem

manzur
Guest
Posts: n/a

 06-06-2006
BigDecimal bigDecimal = new BigDecimal("1.00");
BigDecimal bigDecima2 = new BigDecimal("1");

System.out.println(bigDecimal.equals(bigDecima2));

The above gives me false.

Wht should i do to make it print true with out disturbing my already
created bigdecimals.
Bcoz i feel that 1.0,1.00,1,1.0000 are same

Oliver Wong
Guest
Posts: n/a

 06-06-2006
"manzur" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> BigDecimal bigDecimal = new BigDecimal("1.00");
> BigDecimal bigDecima2 = new BigDecimal("1");
>
> System.out.println(bigDecimal.equals(bigDecima2));
>
> The above gives me false.
>
> Wht should i do to make it print true with out disturbing my already
> created bigdecimals.
> Bcoz i feel that 1.0,1.00,1,1.0000 are same

http://java.sun.com/j2se/1.5.0/docs/...igDecimal.html

They explicitly tell you that equals will return false when comparing
2.0 to 2.00. They also explain how to get the results you want.

Why would the behaviour of 2.0 != 2.00 be useful? Perhaps for
scientific/engineering applications where significant digits are meaningful.

- Oliver

manzur
Guest
Posts: n/a

 06-06-2006

Oliver Wong wrote:
> "manzur" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> > BigDecimal bigDecimal = new BigDecimal("1.00");
> > BigDecimal bigDecima2 = new BigDecimal("1");
> >
> > System.out.println(bigDecimal.equals(bigDecima2));
> >
> > The above gives me false.
> >
> > Wht should i do to make it print true with out disturbing my already
> > created bigdecimals.
> > Bcoz i feel that 1.0,1.00,1,1.0000 are same

>
> http://java.sun.com/j2se/1.5.0/docs/...igDecimal.html
>
> They explicitly tell you that equals will return false when comparing
> 2.0 to 2.00. They also explain how to get the results you want.
>
> Why would the behaviour of 2.0 != 2.00 be useful? Perhaps for
> scientific/engineering applications where significant digits are meaningful.
>
> - Oliver

iam developing some banking applications where i need to show that 1.0
,1.00,1.000 are same

jhr
Guest
Posts: n/a

 06-06-2006

manzur wrote:
> Oliver Wong wrote:
> > "manzur" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) ups.com...
> > > BigDecimal bigDecimal = new BigDecimal("1.00");
> > > BigDecimal bigDecima2 = new BigDecimal("1");
> > >
> > > System.out.println(bigDecimal.equals(bigDecima2));
> > >
> > > The above gives me false.
> > >
> > > Wht should i do to make it print true with out disturbing my already
> > > created bigdecimals.
> > > Bcoz i feel that 1.0,1.00,1,1.0000 are same

> >
> > http://java.sun.com/j2se/1.5.0/docs/...igDecimal.html
> >
> > They explicitly tell you that equals will return false when comparing
> > 2.0 to 2.00. They also explain how to get the results you want.
> >
> > Why would the behaviour of 2.0 != 2.00 be useful? Perhaps for
> > scientific/engineering applications where significant digits are meaningful.
> >
> > - Oliver

>
>
> iam developing some banking applications where i need to show that 1.0
> ,1.00,1.000 are same

Try:

if (bigDecimal.compareTo(bigDecima2) == 0)
{
// code for equals == true
}

Patricia Shanahan
Guest
Posts: n/a

 06-06-2006
Oliver Wong wrote:
> "manzur" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>> BigDecimal bigDecimal = new BigDecimal("1.00");
>> BigDecimal bigDecima2 = new BigDecimal("1");
>>
>> System.out.println(bigDecimal.equals(bigDecima2));
>>
>> The above gives me false.
>>
>> Wht should i do to make it print true with out disturbing my already
>> created bigdecimals.
>> Bcoz i feel that 1.0,1.00,1,1.0000 are same

>
> http://java.sun.com/j2se/1.5.0/docs/...igDecimal.html
>
> They explicitly tell you that equals will return false when comparing
> 2.0 to 2.00. They also explain how to get the results you want.
>
> Why would the behaviour of 2.0 != 2.00 be useful? Perhaps for
> scientific/engineering applications where significant digits are
> meaningful.
>
> - Oliver

At a more basic level, the result of some operations involving a
BigDecimal depend on its scale. toString gives different answers. The
precision is different. Rounded divisions give different answers.

They are just not equivalent objects.

Patricia

Ingo R. Homann
Guest
Posts: n/a

 06-07-2006
Hi,

Patricia Shanahan wrote:
>>> BigDecimal bigDecimal = new BigDecimal("1.00");
>>> BigDecimal bigDecima2 = new BigDecimal("1");
>>>
>>> System.out.println(bigDecimal.equals(bigDecima2));
>>>

>
> At a more basic level, the result of some operations involving a
> BigDecimal depend on its scale. toString gives different answers. The
> precision is different. Rounded divisions give different answers.

I understand that, but I think that is not a reason that "1.0" and
"1.00" should not be equal. I mean, especially...

> They are just not equivalent objects.

....that is (IMHO) not correct: "equivalent" - the same word as in
Germany (although we use the strange "Umlaut" "ä" at the beginning) -
which (AFAIK) comes from the Latin words for "equal" and "value", and I
think, the values of "1.00" and "1.0" *are* the same.

Ciao,
Ingo

Oliver Wong
Guest
Posts: n/a

 06-07-2006

"Ingo R. Homann" <(E-Mail Removed)> wrote in message
news:4486c9a7\$0\$11077\$(E-Mail Removed)-online.net...
> Hi,
>
> Patricia Shanahan wrote:
>>>> BigDecimal bigDecimal = new BigDecimal("1.00");
>>>> BigDecimal bigDecima2 = new BigDecimal("1");
>>>>
>>>> System.out.println(bigDecimal.equals(bigDecima2));
>>>>

>>
>> At a more basic level, the result of some operations involving a
>> BigDecimal depend on its scale. toString gives different answers. The
>> precision is different. Rounded divisions give different answers.

>
> I understand that, but I think that is not a reason that "1.0" and "1.00"
> should not be equal. I mean, especially...
>
>> They are just not equivalent objects.

>
> ...that is (IMHO) not correct: "equivalent" - the same word as in Germany
> (although we use the strange "Umlaut" "ä" at the beginning) - which
> (AFAIK) comes from the Latin words for "equal" and "value", and I think,
> the values of "1.00" and "1.0" *are* the same.

As I've mentioned elsewhere in this newsgroup (but not within this
thread), the concept of equality is context dependent. Are "1.00" and "1.0"
equal? Perhaps yes, if you're talking about pure mathematical numbers.
Perhaps no, if you're talking about engineering measurements with specific
tolerance levels[*]. Definitely not, if you're talking about Strings.

The Java API was designed so that you could provide a comparator object,
so that you could using different metrics for determining how two objects
compare, depending on which metric you're interested in. I thought it might
be a good idea if they did the same thing for the equals method (so you
could provide a custom object to determine whether two objects are equal,
depending on the context). Unfortunately, the API wasn't designed that way.

But moving away from idle daydreaming and coming back to practical
matters: recall that in Java, you're free to implement the equals() method
any way you want, as long as some basic requirements (reflexivity,
transitivity, etc.) are met. The people who designed the BigDecimal class
decided to design it in that particular way. Anyway, it's not like you can't
get the above mentioned desired behaviour: All you have to do is use
compareTo().

- Oliver
[*] An engineer might reject a part that measures "1.0 mm", requiring one
that measures "1.00 mm". Why? Because that engineer might know that if the
part actually measures 1.01 mm (which is considered to be a "1.0 mm" part,
but not a "1.00 mm" part), the device will fail and/or break, whereas if the
device measures 1.004 mm (which is both a "1.0 mm" and "1.00 mm" part), the
device will tolerate this small deviancy, and continue functioning.

Patricia Shanahan
Guest
Posts: n/a

 06-07-2006
Ingo R. Homann wrote:
> Hi,
>
> Patricia Shanahan wrote:
>>>> BigDecimal bigDecimal = new BigDecimal("1.00");
>>>> BigDecimal bigDecima2 = new BigDecimal("1");
>>>>
>>>> System.out.println(bigDecimal.equals(bigDecima2));
>>>>

>>
>> At a more basic level, the result of some operations involving a
>> BigDecimal depend on its scale. toString gives different answers. The
>> precision is different. Rounded divisions give different answers.

>
> I understand that, but I think that is not a reason that "1.0" and
> "1.00" should not be equal. I mean, especially...
>
>> They are just not equivalent objects.

>
> ...that is (IMHO) not correct: "equivalent" - the same word as in
> Germany (although we use the strange "Umlaut" "ä" at the beginning) -
> which (AFAIK) comes from the Latin words for "equal" and "value", and I
> think, the values of "1.00" and "1.0" *are* the same.
>
> Ciao,
> Ingo
>

Anyone who thinks they are equivalent objects, try running this:

import java.math.BigDecimal;
public class TestBigDecimal {
public static void main(String[] args) {
System.out.println(new BigDecimal("1.0").
divide(new BigDecimal(3),BigDecimal.ROUND_HALF_EVEN));
System.out.println(new BigDecimal("1.00").
divide(new BigDecimal(3),BigDecimal.ROUND_HALF_EVEN));
}
}

If they really were equivalent, I would expect them to do the same for
anything other than the identity related operations, == comparison and
System.identityHashCode().

A BigDecimal has both a decimal value and a scale, not just a decimal
value. The scale affects some operations, such as the rounded divisions
in the example program.

Their decimal values are equal, and decimal value is the natural
ordering, so it makes sense for compareTo to treat them as equal.

Patricia

Thomas Weidenfeller
Guest
Posts: n/a

 06-07-2006
Ingo R. Homann wrote:
> I
> think, the values of "1.00" and "1.0" *are* the same.

Not if you are an engineer. 1.00 indicates a higher precision than 1.0.
Depending on the type of rounding used (AFAIK varies in different
countries in engineering), the above numbers e.g. indicate the two
different ranges

1.00: [0.995, 1.005)
1.0: [0.95, 1.05)

for an engineer. And these ranges are rather different.

Stichwort "Signifikante Stellen"

/Thomas
--
The comp.lang.java.gui FAQ:
http://www.uni-giessen.de/faq/archiv....java.gui.faq/

Ingo R. Homann
Guest
Posts: n/a

 06-07-2006
Hi,

Oliver Wong wrote:
>>>>> BigDecimal bigDecimal = new BigDecimal("1.00");
>>>>> BigDecimal bigDecima2 = new BigDecimal("1");
>>>>>
>>>>> System.out.println(bigDecimal.equals(bigDecima2));

> ...
> As I've mentioned elsewhere in this newsgroup (but not within this
> thread), the concept of equality is context dependent. Are "1.00" and
> "1.0" equal? Perhaps yes, if you're talking about pure mathematical
> numbers. Perhaps no, if you're talking about engineering measurements
> with specific tolerance levels

Ah, OK, that is a point I did not consider! I was only thinking of the
"mathematical" equality.

Still, I am not convinced that the two values should not be mathematical
equal (which you did not say), but I see (and agree to) your point, and
it might be enough to say "they are not *generally* equal".

I know that compareTo() does exactly what I want, but I just think,
"b.compareTo(BigDecimal.ZERO)==0" is not as beautiful and readable as
allowed) "b==BigDecimal.ZERO" (or perhaps "b===BigDecimal.ZERO", if you
think that is is not a good idea to allow to overload the
"referential-equality-comparison-operator" ==).

Ciao,
Ingo, daydreaming