Skip to main content

[webtier] JSF Converter question

3 replies [Last post]
Anonymous

Hi all

When I have the following in a form and submit I get an error:

The error is:
value="#{dummy.doubleNumber}": Can't set property 'doubleNumber' on class
'backing.DummyBacking_$$_javassist_6' to value '123,000,000'.)

The doubleNumber property is a Double.

When debugging the NumberConverter the parsed Object seems to be a Long.

Question: is this the expected behaviour?

Ran into this when implementing a "UnitNumberConverter" which will divide
the number by some unit, like 1000,000 to display numbers in for example
millions.

Cheers,
Micke
[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rlubke
Offline
Joined: 2003-08-21
Points: 0

This is per the javadocs for DecimalFormat:

"If isParseBigDecimal() is false (the default), most integer values are returned as Long objects, no matter how they are written: "17" and "17.000" both parse to Long(17). Values that cannot fit into a Long are returned as Doubles. This includes values with a fractional part, infinite values, NaN, and the value -0.0. DecimalFormat does not decide whether to return a Double or a Long based on the presence of a decimal separator in the source string. Doing so would prevent integers that overflow the mantissa of a double, such as "-9,223,372,036,854,775,808.00", from being parsed accurately."

The best way to deal with this is to have your properties accept/return Number instances and then
in your processing logic call Number.doubleValue().

Mikael Andersson

Thanks Ryan.

Guess the key thing here is the Number part of the convertNumber tag :), to
bad though that it doesn't work well with the subclasses.

Cheers

2008/7/14 :

> This is per the javadocs for DecimalFormat:
>
> "If isParseBigDecimal() is false (the default), most integer values are
> returned as Long objects, no matter how they are written: "17" and "17.000"
> both parse to Long(17). Values that cannot fit into a Long are returned as
> Doubles. This includes values with a fractional part, infinite values, NaN,
> and the value -0.0. DecimalFormat does not decide whether to return a Double
> or a Long based on the presence of a decimal separator in the source string.
> Doing so would prevent integers that overflow the mantissa of a double, such
> as "-9,223,372,036,854,775,808.00", from being parsed accurately."
>
> The best way to deal with this is to have your properties accept/return
> Number instances and then
> in your processing logic call Number.doubleValue().
> [Message sent by forum member 'rlubke' (rlubke)]
>
> http://forums.java.net/jive/thread.jspa?messageID=286485
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>
>
[att1.html]

Mikael Andersson

Some more information about what I'm doing.

For example:
dummy.doubleNumber = 123,000,000.10

I want to display this in millions, so I've created my own converter which
extends NumberConverter.

With only maxFractionDigits=3 the displayed value is: "123"

Then on postback I have this in my UnitNumberConverter.getAsObject()
(extends NumberConverter):

Object obj = super.getAsObject(ctx, comp, value);

Unless the "value" String contains something with a decimal point and non
zeroes the returned object is a Long.

I thought I could get around this problem by using the minFractionDigits.
But even if the value coming into getAsObject is "123.00" I get a Long
object back from the super.getAsObject call.

This seems like slightly dodgy behaviour, or perhaps I'm using it the wrong
way.

Cheers,
micke

2008/7/14 Mikael Andersson :

> Hi all
>
> When I have the following in a form and submit I get an error:
>
>
>
>
>

>
> The error is:
> value="#{dummy.doubleNumber}": Can't set property 'doubleNumber' on class
> 'backing.DummyBacking_$$_javassist_6' to value '123,000,000'.)
>
> The doubleNumber property is a Double.
>
> When debugging the NumberConverter the parsed Object seems to be a Long.
>
> Question: is this the expected behaviour?
>
> Ran into this when implementing a "UnitNumberConverter" which will divide
> the number by some unit, like 1000,000 to display numbers in for example
> millions.
>
> Cheers,
> Micke
>
[att1.html]