[play-framework] Decimal values and Locale.

734 views
Skip to first unread message

Renato Oliveira

unread,
Aug 3, 2011, 7:55:38 AM8/3/11
to play-framework
I was wondering, if play shouldn't consider the Locale in order to render and parse decimal values without using a explicit a fotmat operation.

I had a few tests (with I have may missed some) and when I read a value from database, it comes on a 1111.99 format. Since here in brazil the decimal separator is ", ", I have to format it to show it correctly (1111,99).

When I need to save it to the database, I have to format it back to the 1111.99 pattern.

I started thinking that I need to write a binder for that kind of value, but thinking better, I guess it could be something handled by the framework. Maybe using I18N class, something like the date pattern that I can set on application.conf for each language.

Since I already have the locale, I just need a NumberFormat operation.

For now, I use a format operation on my views to show the value as 1.111,99, and I have placed a NumberFormat operation to parse the value with the Locale on the setter to convert it back to 1111.99.
(I really don't like that, but if someday the framework start considering locale for decimals I only need to remove the setter)

Is this something that the framework should take care?

Is other countries like Brazil where the decimal values are in a different pattern?

Thank you.


2011/8/2 Renato Oliveira <ren...@oliveiraonline.com>
Hi Christian.

It will probably solve the validation thing, but I still need to
convert it back to the database pattern with . as a decimal separator.

Thank you.


On 2 ago, 04:57, christian sarnataro <christian.sarnat...@gmail.com>
wrote:
> Did you try with the @Match validation?
>
> http://www.playframework.org/documentation/1.2.2/validation-builtin#m...
>
> You could use a pattern like:
> pattern = "((^\\d{1,3}(\\.\\d{3})+(,\\d+)?)$)";
>
> Bye
>
> On Aug 2, 12:36 am, Renato Oliveira <ren...@oliveiraonline.com> wrote:
>
>
>
>
>
>
>
> > Hi.
>
> > It's my first message here. I've tried to find this over the web but I
> > didn't found anything.
>
> > I need to format a double value with my locale. It works fine for showing
> > the value on a view for example.
>
> > Here in Brazil the a number or currency has this format 9.999,99.
>
> > If I set a lang "pt" on my application.conf, the format of a number on a
> > view is fine (9.999,99).
>
> > I need something similar to my forms and validation.
>
> > If I format the value that I show on the form(so the user can enter the
> > information with the right format), after submit, the validation crashes
> > (invalid number, or something like it).
>
> > I there is any binder @As for a decimal value?
>
> > Or should I implement one?
>
> > Thank you.
>
> > Renato

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.


GrailsDeveloper

unread,
Aug 3, 2011, 8:19:55 AM8/3/11
to play-framework
As mentioned in http://groups.google.com/group/play-framework/browse_thread/thread/b1b7dd45aba66a6c#
I think this should be done by the framework. First look for a pattern
in the application.conf then use the locale defined by the request.
For me it doesn't look like a great implementation effort.

So from me a clear vote that this should be done by the framework.
However I think we need a clear statement from the core team, because
it's not funny to implement it and then only produce a rejected patch.

Perhaps it would be useful to create a ticket.

Niels

Renato Oliveira

unread,
Aug 3, 2011, 9:07:33 AM8/3/11
to play-fr...@googlegroups.com
Sorry I have missed your message on that other thread.

I agree with you.

Looking a little further on your other message.

I guess there is no need to use application.conf.

Since we have the locale on Lang.getLocale() (either from the browser or from the application.langs on application.conf), I guess the only thing we need is to execute a numberformat or decimal format on the binder for float, double and bigdecimal.

Something like this:
DecimalFormat.getInstance(Lang.getLocale()).parse(value).doubleValue(); // double
DecimalFormat.getInstance(Lang.getLocale()).parse(value).floatValue(); // float
(BigDecimal) DecimalFormat.getInstance(Lang.getLocale()).parse(value); // BigDecimal

I will open a ticket, thank you.

Renato

2011/8/3 GrailsDeveloper <openso...@googlemail.com>

GrailsDeveloper

unread,
Aug 3, 2011, 10:08:47 AM8/3/11
to play-framework
I'm unsure if the Lang.getLocale() will be enough. If you have de I'm
unsure if in Swiss the number representation the same as in Germany.
So it would be good if can be configured like the date in
application.conf.
Niels

On 3 Aug., 15:07, Renato Oliveira <ren...@oliveiraonline.com> wrote:
> Sorry I have missed your message on that other thread.
>
> I agree with you.
>
> Looking a little further on your other message.
>
> I guess there is no need to use application.conf.
>
> Since we have the locale on Lang.getLocale() (either from the browser or
> from the application.langs on application.conf), I guess the only thing we
> need is to execute a numberformat or decimal format on the binder for float,
> double and bigdecimal.
>
> Something like this:
> DecimalFormat.getInstance(Lang.getLocale()).parse(value).doubleValue(); //
> double
> DecimalFormat.getInstance(Lang.getLocale()).parse(value).floatValue(); //
> float
> (BigDecimal) DecimalFormat.getInstance(Lang.getLocale()).parse(value); //
> BigDecimal
>
> I will open a ticket, thank you.
>
> Renato
>
> 2011/8/3 GrailsDeveloper <opensourc...@googlemail.com>
>
> > As mentioned in
> >http://groups.google.com/group/play-framework/browse_thread/thread/b1...

T.J. Crowder

unread,
Aug 4, 2011, 6:28:01 AM8/4/11
to play-framework
On Aug 3, 3:08 pm, GrailsDeveloper <opensourc...@googlemail.com>
wrote:
> I'm unsure if the Lang.getLocale() will be enough. If you have de I'm
> unsure if in Swiss the number representation the same as in Germany.
> So it would be good if can be configured like the date in
> application.conf.
> Niels

The Java Locale class[1] goes beyond language. As you say, the German-
speaking Swiss and the German-speaking Germans don't do things the
same way, nor the French speakers in Quebec, Paris, and Brussels, nor
the English speakers in L.A. and London (in oh so many ways).

Java's Locale class handles country (and also variant) as well as
language, but I'm not immediately seeing how (if) Play! handles it.
I've posted my own question asking about that.

[1] http://download.oracle.com/javase/6/docs/api/java/util/Locale.html

FWIW,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com
Reply all
Reply to author
Forward
0 new messages