Re: Custom label for number and currency formatting?
Label dbl = new Label(dbllbl,+x){ Why pass in a string? Better is to do new Label(foo, new Model(x)); @Override protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) { Object val = getConverter().convert(getModelObjectAsString(), If you are merely displaying the value, you don't need to convert. What this line above does - if you'd pass in a model that produces a number) is convert from a number to a string (using the converter, this is in getModelObjectAsString) and back again. You can just do Object val = getModelObject(). Alternatives: 1) Wrap the model (decorator pattern) so that it returns the formatted value. 2) Use a custom converter like: new Label(foo, new Model(x)) { public IConverter getConverter(Class type) { return new AbstractNumberConverter() { public NumberFormat getNumberFormat(Locale locale) { return NumberFormat.getCurrencyInstance(); } } } } Eelco - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Custom label for number and currency formatting?
Thanks for the ideas. Here is my implementation. I split it into two classes since I may want to use the CurrencyConverter separately. In my initial testing it works well, but I haven't tested convertToObject(). Let me know if anyone sees any issues or has suggested improvements: public class CurrencyConverter extends AbstractNumberConverter { private static final long serialVersionUID = 1L; public CurrencyConverter() { } @Override public NumberFormat getNumberFormat(Locale locale) { return NumberFormat.getCurrencyInstance(locale); } @Override public Class getTargetType() { return Double.class; } public Object convertToObject(String value, Locale locale) { // string to double. try { return getNumberFormat(locale).parse(value).doubleValue(); } catch (ParseException ex) { throw new WicketRuntimeException(exception when trying to parse currency value: + ex.getMessage()); } } @Override public String convertToString(Object value, Locale locale) { // double to string return getNumberFormat(locale).format(value); } } public class CurrencyLabel extends Label { private static final long serialVersionUID = 1L; public CurrencyLabel(String id) { this(id,null); } public CurrencyLabel(String id, IModel model) { super(id,model); } public IConverter getConverter(Class type) { return new CurrencyConverter(); } } Anyone can feel free to use this or add it to Wicket core. Tauren On 10/5/07, Eelco Hillenius [EMAIL PROTECTED] wrote: Label dbl = new Label(dbllbl,+x){ Why pass in a string? Better is to do new Label(foo, new Model(x)); @Override protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) { Object val = getConverter().convert(getModelObjectAsString(), If you are merely displaying the value, you don't need to convert. What this line above does - if you'd pass in a model that produces a number) is convert from a number to a string (using the converter, this is in getModelObjectAsString) and back again. You can just do Object val = getModelObject(). Alternatives: 1) Wrap the model (decorator pattern) so that it returns the formatted value. 2) Use a custom converter like: new Label(foo, new Model(x)) { public IConverter getConverter(Class type) { return new AbstractNumberConverter() { public NumberFormat getNumberFormat(Locale locale) { return NumberFormat.getCurrencyInstance(); } } } } Eelco - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Custom label for number and currency formatting?
Does such a thing exist? I thought it did, but can't find it now. Basically, I want a Label that allows me to format floats/doubles as currency. I don't need i18n built in (for different currencies, etc.), just take double x=8.3 and output $8.30. Any pointers are appreciated! Thanks. Tauren - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Custom label for number and currency formatting?
Do this double x= 8.3; Label dbl = new Label(dbllbl,+x){ @Override protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag) { Object val = getConverter().convert(getModelObjectAsString(), Double.class); final NumberFormat nf = NumberFormat.getCurrencyInstance(); String newval = nf.format(val); replaceComponentTagBody(markupStream, openTag, newval); } }; If you want to vary the locale on some cond, just use the method getCurrencyInstance(Locale inLocale) in NumberFormat class -swaroop tauren wrote: Does such a thing exist? I thought it did, but can't find it now. Basically, I want a Label that allows me to format floats/doubles as currency. I don't need i18n built in (for different currencies, etc.), just take double x=8.3 and output $8.30. Any pointers are appreciated! Thanks. Tauren - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- View this message in context: http://www.nabble.com/Custom-label-for-number-and-currency-formatting--tf4564849.html#a13032472 Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]