dev@javaserverfaces.java.net

NumberConverter using buggy NumberFormat ?

From: Matthias Wessendorf <matzew_at_apache.org>
Date: Fri, 30 Nov 2007 19:38:09 +0100

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

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