We keep all our money amounts as ints representing the number of cents.
We have a MoneyField that extends TextField and overrides
getConverter(Class) to return something like this:

public class MoneyConverter implements IConverter {

    public Object convertToObject(String value, Locale locale) {
        try {
            return Math.round(Float.parseFloat(value) * 100);
        } catch (NumberFormatException e) {
            throw new ConversionException(e)
                .setSourceValue(value)
                .setResourceKey("MoneyConverter")
        }
    }

    public String convertToString(Object value, Locale locale) {
        float amount = ((Number) value).floatValue() / 100;
        return String.format("%.2f", amount);
    }
}

If you like, you can prepend a dollar sign in convertToString and strip
it in convertToObject.

One thing I want to do but haven't gotten around to is to change the
styling depending on the focus. When the field doesn't have focus it
should be right-aligned, but this is weird when editing, so the idea is
to left-align it while the field has focus.

jk

On Wed, Feb 25, 2009 at 07:20:31PM -0300, Daniel Ferreira Castro wrote:
> I would like an opinion about what is the propper, or more recommended, way
> to format a TextField to show money?
> Should I declare it TextField<String> or TextField<Float>?
> 
> And to format it with the money symbol ( Like US$) while you type?  Any
> ideas?
> 
> -- 
> "Two rules to succeed in life:
> 1 - don“t tell people everything you know."
> --------
> We shall go on to the end.
> We shall fight in France
> We shall fightover the seas and oceans.
> We shall fight with growing confidence and growing strength in the air.
> We shall defend our island whatever the cost may be
> We shall fight on beaches, we shall fight on the landing grounds,
> We shall fight in the fields and in the streets,
> We shall fight on the hills.
> We shall never surrender.
> Winston Churchill

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to