Double formatting in Java

Asked

Viewed 3,987 times

5

I have the following value 1234.0000. I would like to format to the following output: 1.234,00. There may be cases I’ll use as well 1.234,0, but I believe that making for the first case the others are similar.

  • Something tells me that there are some duplicates of this question already answered.

  • I found only in c#

  • 1

    @Murilofechio see if it can help you here. http://answall.com/questions/6691/problemas-para-imprimir-um-numero-double/6697#6697

  • You don’t give @Marconi, because you don’t put . in the thousands tab

  • Related: http://answall.com/q/47102/101. If no one can find a dup, I can answer but I’d like to find it before I risk it. Is this dup? http://answall.com/q/16276/101

  • @bigown, tested the 3 answers of the question quoted by @Marconi for the value 1234.000 that I spoke. The results I obtained were : 1234.0, 1234.0, 1234, that is not my expected result.

Show 1 more comment

2 answers

7


You can use Decimalformat:

double d = 1243123.1;
DecimalFormat df = new DecimalFormat("###,###.00");
System.out.println(df.format(d));

Exit:

1.243.123,10

However Decimalformat uses the properties of your operating system to define the output format, for example, in Brazil we use , to separate the whole number from the decimal places, already in the USA the , serves to separate the thousands. In my OS the output was as shown above, but depending on the OS can come out a little different.

If you don’t want to allow these OS variations, you can use the Locale class to locate your output, and you can even add the currency to the output if you’re working with cash.

Example:

double d = 1243123.1;

Locale localeBR = new Locale( "pt", "BR" );  
NumberFormat dinheiroBR = NumberFormat.getCurrencyInstance(localeBR);  
System.out.println(dinheiroBR.format(d));

Locale localePT = new Locale( "pt", "PT" );  
NumberFormat dinheiroPT = NumberFormat.getCurrencyInstance(localePT);  
System.out.println(dinheiroPT.format(d));

NumberFormat numeroBR = NumberFormat.getNumberInstance(localeBR);
numeroBR.setMinimumFractionDigits(2);
numeroBR.setMaximumFractionDigits(2);
System.out.println(numeroBR.format(d));

Locale localeUS = new Locale( "en", "US" );  
NumberFormat numeroUS = NumberFormat.getNumberInstance(localeUS);
numeroUS.setMinimumFractionDigits(2);
numeroUS.setMaximumFractionDigits(2);
System.out.println(numeroUS.format(d));

Exit:

R$ 1,243,123.10
1.243.123,10 €
1.243.123,10
1,243,123.10

  • my result here was 1,243,123.10

  • @Murilofechio um, must be dependent on the OS, and yours is probably in the US pattern. You wanted in the pattern I put in the answer, correct?

2

I decided by setting characters to separate decimal places and groups:

    double d = 11356982.10000;

    DecimalFormat df = new DecimalFormat("###,###.00");
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();

    //define o caractere separador das casas decimais
    dfs.setDecimalSeparator(',');
    //define o caractere separador dos grupos das milhares
    dfs.setGroupingSeparator('.');
    //seta o formatador de simbolos ao formatador do decimal
    df.setDecimalFormatSymbols(dfs);

    String total = df.format(d);

For the value 11356982.10000 outworking 11.356.982,10

  • If you decided to mark the correct answer for future visitors to this topic. The @Math response helped, so I think the correct answer would be his.

Browser other questions tagged

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