0

I developed a web application in Java8 under Windows7, I generated the war file and I deployed it on Tomcat8 under Ubuntu; everything works fine but when I format and print numbers I get different outputs. In Windows for example I print "74,00" but in Linux i get "74..00". Here is my code:

// call method

formatCurrency(String.format("%.2f", amount));

// method

private String formatCurrency(String number){
    String formattedNumber = "";
    try {
        String[] support = number.split(",");
        String intero = support[0];
        if (intero.length() > 3){
            formattedNumber = intero.substring(0, intero.length() - 3) + "." + intero.substring(intero.length() - 3);
            if (intero.substring(0, intero.length() - 3).length() > 3){
                formattedNumber = intero.substring(0, intero.length() - 6) + "." + formattedNumber;
            }
        }
        else
            formattedNumber = support[0];

        formattedNumber = formattedNumber + "," + support[1];
    }
    catch (Exception ex) {
        System.out.println("errore in stampa " + ex);
    }
    return formattedNumber;
}
4
  • Well, I'd start that your String number smells like contradiction to me Commented Mar 13, 2017 at 13:38
  • I have to print a number, so I call it "number" even if it's a String, functionally it is a number ... a part from the naming convention, can you help me? Commented Mar 13, 2017 at 13:42
  • what's your input ? Also if you log input and output in the code that will help you figure out the issue quickly Commented Mar 13, 2017 at 13:42
  • thank you for your advice; the input "amount" is a double Commented Mar 13, 2017 at 13:47

1 Answer 1

2

By default Java's formatting functions will use the default locale, and it's very probable that the default locale on your Windows system and your Linux system are different. You should use the variant that allows you to specify the desired locale

formatCurrency(String.format(Locale.US,"%.2f", amount));

Obviously, use the proper Locale for your usecase.

As a side note, isn't that code extremely convoluted?


If - as I suspect - the only goal is to obtain thousands separators, may I suggest this alternative that's a lot simpler and much less error-prone?

private String formatCurrency(double amount) {
    NumberFormat nf = NumberFormat.getInstance(Locale.ITALIAN);
    nf.setGroupingUsed(true); //force thousands separators, for Locale.ITALIAN it's not even needed
    return nf.format(amount);
}
Sign up to request clarification or add additional context in comments.

4 Comments

thank you very much, problem solved with "Locale.ITALY" ... thanks thanks thanks!
Glad it helped you. To get back to my last remark, am I right in assuming that all that code is just there to put thousands separators in place?
yes you're right, thank you for your code snippet, I didn't know that library
If you feel my answer is correct, and no better answers arrive within a few days, may I suggest that you accept the answer? That way it's clearer for future visitors that the answer is usable, without having to plow through the comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.