You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by mitroiasi <mi...@yahoo.com> on 2009/04/29 09:52:00 UTC

BigDecimal or Double


Hi,

I have some input fields which should accept only numbers with 2 decimals.
My question is: Which is better to use - BigDecimal or Double?
I know that for financial application it is better to use BigDecimal types
for the computation.

I have tried both in my application and I will explain what happens:
When I use Double, the following line of code works:
<f:convertNumber pattern="###,###,###,##0.00" minIntegerDigits="1"
maxIntegerDigits="12" maxFractionDigits="2" />

When I use BigDecimal I must use the old converter:
<s:convertNumber pattern="###,###,###,##0.00" minIntegerDigits="1"
maxIntegerDigits="12" maxFractionDigits="2" />, but then the input field
react strange. I try to insert one number and when I insert a comma, the
comma is shown before number. If I continue inserting the decimals, the
entire number is shown correctly.

Another problem of mine is the fact that before I have used in my
application int and double. As i said before the convertNumber works for
double, but it doesn't work for int.

Which is the best approach?
BigDecimal or double?
BigDecimal or Number or int?


-- 
View this message in context: http://www.nabble.com/BigDecimal-or-Double-tp23292320p23292320.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: BigDecimal or Double

Posted by Matthias Wessendorf <ma...@apache.org>.
On Wed, Apr 29, 2009 at 9:52 AM, mitroiasi <mi...@yahoo.com> wrote:
>
>
> Hi,
>
> I have some input fields which should accept only numbers with 2 decimals.
> My question is: Which is better to use - BigDecimal or Double?
> I know that for financial application it is better to use BigDecimal types
> for the computation.
>
> I have tried both in my application and I will explain what happens:
> When I use Double, the following line of code works:
> <f:convertNumber pattern="###,###,###,##0.00" minIntegerDigits="1"
> maxIntegerDigits="12" maxFractionDigits="2" />
>
> When I use BigDecimal I must use the old converter:
> <s:convertNumber pattern="###,###,###,##0.00" minIntegerDigits="1"
> maxIntegerDigits="12" maxFractionDigits="2" />, but then the input field
> react strange. I try to insert one number and when I insert a comma, the
> comma is shown before number. If I continue inserting the decimals, the
> entire number is shown correctly.
>
> Another problem of mine is the fact that before I have used in my
> application int and double. As i said before the convertNumber works for
> double, but it doesn't work for int.
>
> Which is the best approach?
> BigDecimal or double?
> BigDecimal or Number or int?

Here is a related issue:
https://issues.apache.org/jira/browse/MYFACES-1890
to have a better BigDecimal support...

By default the underlying DecimalFormat class returns a double.
When the flag is set, it now returns BigDecimal.

Also, have in mind that BigDecimal cause problems on the JSF validation, since
that is tied to bd1.equals(bd2) and not compareTo(). that means 2.0 != 2.00

-Matthias

>
>
> --
> View this message in context: http://www.nabble.com/BigDecimal-or-Double-tp23292320p23292320.html
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>
>



-- 
Matthias Wessendorf

blog: http://matthiaswessendorf.wordpress.com/
sessions: http://www.slideshare.net/mwessendorf
twitter: http://twitter.com/mwessendorf