Formatting values in the millions

Asked

Viewed 218 times

3

I want to format values in the millions. Example:

The result of a calculation would be 1.960,35. But the way out is 19603565.2315789.

I’ve been using DecimalFormat, but it was for smaller numbers. How to do this in the millions?

  • 1

    How are you doing? Add the code to the question.

  • The hard part is finding, but you already have some answers that show it. Either shows the code you are using.

  • 1

    Is this "." in the example number you quoted in the right place? Wouldn’t it be 1960.35652315789?

  • Show the code and explain why you’re not getting it.

1 answer

3


You can use the class NumberFormat, to format. Example:

    double num = 12345678.90;
    Locale ptBr = new Locale("pt", "BR"); //define a região
    NumberFormat moeda = NumberFormat.getCurrencyInstance(ptBr);
    System.out.print(moeda.format(num)); 

Or all in one line:

System.out.print(NumberFormat.getCurrencyInstance(ptBr).format(num));
  • Sorry it took me so long to answer. @Iron Man, that’s just what I needed. Giving one more search I saw this Numberformat, but I did not know of this option Locale(en), the result only came out in dollars. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.