You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Tauren Mills <ta...@tauren.com> on 2007/10/04 00:55:52 UTC
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Custom label for number and currency formatting?
Posted by Thies Edeling <th...@rrm.net>.
I'd rather wrap it in a model. That way you're not limited to use it in
a Label but can pass it as a parameter
to a StringResourceModel etc. as well. For example source:
http://dev.ehour.nl/fisheye/changelog/eHour/?cs=433
Tauren Mills wrote:
> 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 <ee...@gmail.com> 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: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Custom label for number and currency formatting?
Posted by Tauren Mills <ta...@tauren.com>.
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 <ee...@gmail.com> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Custom label for number and currency formatting?
Posted by Eelco Hillenius <ee...@gmail.com>.
> 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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Custom label for number and currency formatting?
Posted by swaroop belur <sw...@gmail.com>.
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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
>
--
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org