You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Joshua Long <lh...@adelphia.net> on 2005/04/18 01:57:17 UTC

Number validator (admittedly, minutia) problem..

Hiya guys 

Here is my (admittedly stupid) problem: 

I have a field with a validator of 

org.apache.tapestry.valid.NumberValidator

and a value type of 'double'  -- 
it shows a value of 1234.0 -- and what I need is 1234.00 -- I literally
need to set the precision of the case where the value has no decimal
values. 

1234.22 stays and works just fine and updates my paramter just fine.

1234 renders as 1234.0  -- therein is the problem. It needs to be
1234.00 !

Customer is always right, I guess.. I'm sorry to even ask, but is there
an elegant way to solve this problem that I am missing short of writing
a custom
DoubleWithPrecisionValidator ? 

Josh?



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Number validator (admittedly, minutia) problem..

Posted by Todd O'Bryan <to...@mac.com>.
If this is a money field, might I suggest that you not store it as a 
double, but instead store it as an int (for pennies) or some other 
accurate decimal encoding (BigDecimal comes to mind) so that you don't 
get annoying round-off errors that you might not expect.

If you choose an int, you can display it as

int/100 + "." + int%100

If you want to use BigDecimal or double, you can use a NumberFormat to 
correctly put in the right number of decimal places.

HTH,
Todd

On Apr 17, 2005, at 7:57 PM, Joshua Long wrote:

> Hiya guys
>
> Here is my (admittedly stupid) problem:
>
> I have a field with a validator of
>
> org.apache.tapestry.valid.NumberValidator
>
> and a value type of 'double'  --
> it shows a value of 1234.0 -- and what I need is 1234.00 -- I literally
> need to set the precision of the case where the value has no decimal
> values.
>
> 1234.22 stays and works just fine and updates my paramter just fine.
>
> 1234 renders as 1234.0  -- therein is the problem. It needs to be
> 1234.00 !
>
> Customer is always right, I guess.. I'm sorry to even ask, but is there
> an elegant way to solve this problem that I am missing short of writing
> a custom
> DoubleWithPrecisionValidator ?
>
> Josh?
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: custom NumberValidator

Posted by Paul Ferraro <pm...@columbia.edu>.
Look at org.apache.tapestry.form.translator.NumberTranslator in the 4.0 
branch.
It should be easy enough to adapt this to the 3.0 IValidator interface.

Paul

Eric Schneider wrote:

> Hi,
>
> Has anyone created a subclass of NumberValidator that includes a  
> format attribute similar to the DateValidator implementation?
>
> I need ValidFields to take numeric values formatted like: "150,000"
>
> Just figured I'd take a stab before getting dirty.
>
> Thanks,
> Eric
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


custom NumberValidator

Posted by Eric Schneider <er...@centralparksoftware.com>.
Hi,

Has anyone created a subclass of NumberValidator that includes a  
format attribute similar to the DateValidator implementation?

I need ValidFields to take numeric values formatted like: "150,000"

Just figured I'd take a stab before getting dirty.

Thanks,
Eric

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Number validator (admittedly, minutia) problem..

Posted by Paul Ferraro <pm...@columbia.edu>.
I would recommend writing your own IValidator implementation that uses a 
java.text.DecimalFormat object to format and parse your values.  
Regrettably, Tapestry 3.0.x is missing some obvious IValidators like 
this one.  Part of the reason, I think, is that there is so much 
duplication across implementations (e.g. every validator has a minLength 
property).
In the works for Tapestry 4.0 is a Translator interface for text fields 
(i.e. translation will be decoupled from validation) and will include a 
DecimalFormat-based Translator implementation.

Paul

Joshua Long wrote:

>Hiya guys 
>
>Here is my (admittedly stupid) problem: 
>
>I have a field with a validator of 
>
>org.apache.tapestry.valid.NumberValidator
>
>and a value type of 'double'  -- 
>it shows a value of 1234.0 -- and what I need is 1234.00 -- I literally
>need to set the precision of the case where the value has no decimal
>values. 
>
>1234.22 stays and works just fine and updates my paramter just fine.
>
>1234 renders as 1234.0  -- therein is the problem. It needs to be
>1234.00 !
>
>Customer is always right, I guess.. I'm sorry to even ask, but is there
>an elegant way to solve this problem that I am missing short of writing
>a custom
>DoubleWithPrecisionValidator ? 
>
>Josh?
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org