Search code examples
javanumberformatexception

Handle different numbers with dots and colons in Java


I have these two values: Value 1: "33445.0" Value 2: "33.445,00"

What is the easiest way to see if these two is equal in Java?

I'm struggling with the different types of int, double, float and so on. Now value 1 is stored as double and value 2 is a float.

How can i make these values the same?

Kind regards


Solution

  • You should parse the numbers. The only difference between the two numbers is that they are formatted in different locales

    NumberFormat format_english = NumberFormat.getInstance(Locale.ENGLISH);
    NumberFormat format_france = NumberFormat.getInstance(Locale.GERMAN);
    Number number1 = format.parse("33445.0");
    Number number2 = format.parse("33.445,00");
    double d1 = number1.doubleValue();  // or whatever value you're looking for
    double d2 = number2.doubleValue();
    // now compare them as any two "normal" numbers
    

    Now, how do you know which number goes to which Locale? That's going to be the tricky part. I'm hoping that you don't get the numbers in without any "extra" information about their origins.