dev@javaserverfaces.java.net

Re: NumberConverter using buggy NumberFormat ?

From: Matthias Wessendorf <matzew_at_apache.org>
Date: Mon, 3 Dec 2007 15:49:11 +0100

and... check if prefix/suffix aren't null,
since replace throws a NPE ;-)

On Dec 3, 2007 3:47 PM, Matthias Wessendorf <matzew_at_apache.org> wrote:
> I have an ugly "solution"...
>
> remove (negative/positive) prefix and suffix;
> replace the spaces with the non-braking space (which is the groupingSeparator)
> Attach the prefix and the suffix to the VALUE.
>
> Parse the *modified* VALUE;
>
>
> A demo, that proofs it:
>
> String value = "-12 345,68 ";
> NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.FRANCE);
> DecimalFormat df = (DecimalFormat)nf;
>
> String suffix = null;
> String prefix = null;
> if(value.indexOf(df.getNegativeSuffix())!=-1 &&
> value.indexOf(df.getNegativePrefix())!=-1)
> {
> suffix = df.getNegativeSuffix();
> prefix = df.getNegativePrefix();
> }
> else if(value.indexOf(df.getPositiveSuffix())!=-1 &&
> value.indexOf(df.getPositivePrefix())!=-1)
> {
> suffix = df.getPositiveSuffix();
> prefix = df.getPositivePrefix();
> }
>
> value = value.replace(suffix, "");
> value = value.replace(prefix, "");
> value = value.replace(' ', '\u00a0');
>
> value = prefix+value+suffix;
> ParsePosition pp = new ParsePosition(0);
> Number n = (Number) nf.parseObject(value, pp);
> System.out.println(n);
>
>
>
> On Nov 30, 2007 8:55 PM, Ryan Lubke <Ryan.Lubke_at_sun.com> wrote:
> > Matthias Wessendorf wrote:
> > > thx,
> > >
> > > did you guys worked around it ?
> > >
> > From a faces perspective, no, not yet.
> > Will be thinking about it though, and will post here if/when I have some
> > ideas. If anyone else has ideas, don't be shy.
> >
> >
> > > -m
> > >
> > > On Nov 30, 2007 7:53 PM, Ryan Lubke <Ryan.Lubke_at_sun.com> wrote:
> > >
> > >> Matthias Wessendorf wrote:
> > >>
> > >>> Hi Ryan,
> > >>>
> > >>> I noticed some strange things, when using the NumberConverter for:
> > >>> -currency
> > >>> -fr_FR locale
> > >>>
> > >>> I guess it is a bug in the underlying JDK (1.5.0_11 I am using)........
> > >>>
> > >>> I did a quick JUnit test-case against the MyFaces API (which contains
> > >>> the *base* number-converter)
> > >>>
> > >>> protected void setUp() throws Exception
> > >>> {
> > >>> super.setUp();
> > >>>
> > >>> mock = new NumberConverter();
> > >>> mock.setLocale(Locale.FRANCE);
> > >>> FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.GERMANY);
> > >>> }
> > >>>
> > >>> public void testFranceLocale()
> > >>> {
> > >>> UIInput input = new UIInput();
> > >>> mock.setType("currency");
> > >>> Number number = (Number)
> > >>> mock.getAsObject(FacesContext.getCurrentInstance(), input, "12 345,68
> > >>> ");
> > >>> assertNotNull(number);
> > >>> }
> > >>>
> > >>> And............ it fails :-)
> > >>>
> > >>> So... what is the work-around?
> > >>> I assume it is not to not use fr_FR :-))
> > >>>
> > >>> Also, a simple Java-test fails and shows why:
> > >>>
> > >>> Doing this:
> > >>>
> > >>> String va = "12 345,68 ";
> > >>> NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.FRANCE);
> > >>> Number n = (Number) nf.parseObject(va);
> > >>>
> > >>> and you'll see that n is NULL.
> > >>>
> > >>> Why?
> > >>> So, here it is:
> > >>> the String va contains two blanks (" "), which are between 2 and 3, and
> > >>> between 8 and as well.
> > >>>
> > >>> In fr_FR, however, the *grouping separator * is not " ", but it is a
> > >>> special char for blank (\u00a0).
> > >>> So, my little test will pass, when the first BLANK is replaced by the
> > >>> special char...
> > >>>
> > >>> I thought, that the NumberFormat actually does parse the object for me!!
> > >>> Looks like for the "fr_FR" locale, I have to create a *custom parser*...
> > >>> Which is odd, IMO
> > >>>
> > >>> Now, do this:
> > >>>
> > >>> String va1 = "12 345,68 ";
> > >>> NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.FRANCE);
> > >>> String va2 = nf.format(12345.68));
> > >>> System.out.println(va1.equals(va2));
> > >>>
> > >>> and you see, what the issue from another side :-)
> > >>>
> > >>> Any ideas?
> > >>> (Or perhaps known bugs?)
> > >>>
> > >>> Thx,
> > >>> Matthias
> > >>>
> > >>>
> > >>>
> > >> Hi Mathias,
> > >>
> > >> This looks pretty similar to http://bugs.sun.com/view_bug.do?bug_id=6318800
> > >>
> > >>
> > >> ---------------------------------------------------------------------
> > >> To unsubscribe, e-mail: dev-unsubscribe_at_javaserverfaces.dev.java.net
> > >> For additional commands, e-mail: dev-help_at_javaserverfaces.dev.java.net
> > >>
> > >>
> > >>
> > >
> > >
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe_at_javaserverfaces.dev.java.net
> > For additional commands, e-mail: dev-help_at_javaserverfaces.dev.java.net
> >
> >
>
>
>
>
> --
> Matthias Wessendorf
>
> further stuff:
> blog: http://matthiaswessendorf.wordpress.com/
> sessions: http://www.slideshare.net/mwessendorf
> mail: matzew-at-apache-dot-org
>



-- 
Matthias Wessendorf
further stuff:
blog: http://matthiaswessendorf.wordpress.com/
sessions: http://www.slideshare.net/mwessendorf
mail: matzew-at-apache-dot-org