Setting the Decimal Separator in Java

I was asked to solve a problem of parsing a float value in dutch format (9,6 instead of 9.6). The former would always parse as a whole number, 96 instead of 9.6). I read on the
documentation from sun, http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormatSymbols.html and I saw that a method called setDecimalSeparator(char decimalSeparator) was
available. Lucky for me!

I then opened netbeans and came up with the code below.

Normal 0 false false false MicrosoftInternetExplorer4 Normal 0 false false false MicrosoftInternetExplorer4

package decimalformattest;

 

import java.text.DecimalFormat;

import java.text.DecimalFormatSymbols;

import java.text.Format;

import java.text.ParseException;

import java.util.Locale;

import java.util.logging.Logger;

 

/**

 *

 * @author lamia

 */

public class Main {

 

    Logger logger = Logger.getLogger(Main.class.getName());

 

    public static void main(String[] args) {

        Locale locale = new Locale("nl-NL");

        DecimalFormatSymbols dfSymbols = new DecimalFormatSymbols(locale);

        dfSymbols.setDecimalSeparator(',');

        Format format = new DecimalFormat("#,###,###,##0.00#############", dfSymbols);

        try {

            Object parseObject = format.parseObject("9,5");

            System.out.println(parseObject);

        }

        catch (ParseException ex) {

            ex.printStackTrace();

        }

 

    }

 

}

Output:

run:
9.5
BUILD SUCCESSFUL (total time: 0 seconds)

try removing dfSymbols.setDecimalSeparator(',') and you'll see that it parses differently.

Output without dfSymbols.setDecimalSeparator(',')

run:
95
BUILD SUCCESSFUL (total time: 0 seconds)

Now, I had to translate this to something that Spring would understand. Say, I have a Spring app called my-app. In myapp-servlet.xml we had an old reference to java.text.DecimalFormatSymbols which also had a reference to a Locale object

Normal 0 false false false MicrosoftInternetExplorer4

    <bean id="locale" class="java.util.Locale">

        <constructor-arg index="0">

            <value>"nl-NL"</value>

        </constructor-arg>

        <constructor-arg index="1">

            <value>"NL"</value>

        </constructor-arg>

    </bean>

 

    <bean id="dfSymbols" class="java.text.DecimalFormatSymbols">

        <constructor-arg>

            <ref bean="locale"/>

        </constructor-arg>      

    </bean>


   
So I just added a new property

Normal 0 false false false MicrosoftInternetExplorer4

 

        <property name="decimalSeparator">

          <value>,</value>

        </property>    


       
Which makes dfSymbols bean look like this:

Normal 0 false false false MicrosoftInternetExplorer4

 

 

    <bean id="dfSymbols" class="java.text.DecimalFormatSymbols">

        <constructor-arg>

            <ref bean="locale"/>

        </constructor-arg>      

      

        <property name="decimalSeparator">

          <value>,</value>

        </property>           

    </bean>

 

Hope that helps!Wink

Published 07-29-2009 9:41 PM by lamia
Filed under: