Dimitri
Dimitri

Reputation: 687

Formatting Double with two dp

From my decimalForm method below, i want to convert double value 7777.54 to 7 777,54 but i am getting 7 777 54. what have missed ? result should be 7 777,54

public static String decimalForm(double value){

            DecimalFormat df = new DecimalFormat("#,###,###.00");
            String formatted_value = df.format(value).replaceAll(",", " ").replace(".", ",");

            return formatted_value;
        }

Upvotes: 0

Views: 174

Answers (4)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136062

This is no String.replace version

    DecimalFormat df = new DecimalFormat("#,###,###.00", new DecimalFormatSymbols(Locale.FRANCE));
    String s = df.format(7777.54);
    System.out.println(s);

output

7 777,54

Upvotes: 0

BackSlash
BackSlash

Reputation: 22243

This works for me:

DecimalFormat df = new DecimalFormat("#,###,###.00");
String formatted_value = df.format(value).replaceAll("\\.", " ");

In fact i tried to print out df.format(value) and, with value=95871 i got 95.871,00

Upvotes: 1

obourgain
obourgain

Reputation: 9356

In the pattern #,###,###.00, . is the decimal separator and , is the group separator. The character used for this two separators depends on your locale.

For example, if you are french, df.format(value) will equals to 7 777,54.

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157457

you can use

 String formatted_value = String.format("$%.2f", value);

Upvotes: 0

Related Questions