Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java DecimalFormat problem

I would like to format numbers of type double with a German locale in Java. However something goes wrong since the output of the following code is : 0.0

package main;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

public class Test {

    private static String decimal2Format = "000.000"; 

    public static void main(String args[]){
        DecimalFormat format = (DecimalFormat)NumberFormat.getInstance(new Locale("de"));
        double value = 0;
        try {
            format.applyPattern(decimal2Format);
            value = format.parse("0.459").doubleValue();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        System.out.println(value);
    }

}

Do you have any ideas what is wrong or missing?

Thanks, atticus

like image 872
Atticus Avatar asked Jan 25 '26 21:01

Atticus


1 Answers

You're trying to parse using a pattern which will expect a comma (as it's in German) but you've given it a period ("0.459"). It looks like DecimalFormat stops parsing when it sees a character it doesn't understand. If you change it to "0,459" you'll see it parse correctly and then output "0.459". (I'm not sure whether System.out.println uses the system default locale, in which case it might print "0,459" depending on your locale.)

Note that you haven't tried to format the number at all in this code - only parse a number. If you want to format it, call format. The double itself doesn't have an associated format - it's just a number. It's not like parsing using a particular formatter returns a value which retains that format.

Here's code which will perform actual formatting of a double value:

DecimalFormat format = (DecimalFormat)NumberFormat.getInstance(new Locale("de"));
double value = 0.459;
String formatted = format.format(value);
System.out.println(formatted); // Prints "0,459"

EDIT: Okay, so it sounds like you're converting it from one format to another (from US to European, for example). That means you should probably use two different DecimalFormat objects - you could switch the locale between calls, but that sounds a bit grim to me.

I believe one way to parse and detect errors is to use the parse overload which takes a ParsePosition as well. Set the position to 0 to start with, and afterwards check that it's at the end of the string - if it isn't, that means parsing has effectively failed. I find it odd that there isn't a method which does this automatically and throws an exception, but I can't see one...

You may also want to set the parser to produce a BigDecimal instead of a double, if you're dealing with values which are more logically decimal in nature. You can do this with the setParseBigDecimal method.

like image 83
Jon Skeet Avatar answered Jan 27 '26 09:01

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!