How to change the decimal separator of DecimalFormat from comma to dot/point?

MiraFayless :

I have this little crazy method that converts BigDecimal values into nice and readable Strings.

private String formatBigDecimal(BigDecimal bd){
    DecimalFormat df = new DecimalFormat();
    df.setMinimumFractionDigits(3);
    df.setMaximumFractionDigits(3);
    df.setMinimumIntegerDigits(1);
    df.setMaximumIntegerDigits(3);
    df.setGroupingSize(20);
    return df.format(bd);
}

It however, also produces a so called grouping separator "," that makes all my values come out like this:

xxx,xxx

I do need the separator to be a dot or a point and not a comma. Does anybody have a clue of how to accomplish this little feat?

I have read this and in particular this to death now but I cannot find a way to get this done. Am I approaching this the wrong way? Is there a much more elegant way of doing this? Maybe even a solution that accounts for different local number representations, since the comma would be perfect by European standards.

Chris :

You can change the separator either by setting a locale or using the DecimalFormatSymbols.

If you want the grouping separator to be a point, you can use an european locale:

NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
DecimalFormat df = (DecimalFormat)nf;

Alternatively you can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers produced by the format method. These symbols include the decimal separator, the grouping separator, the minus sign, and the percent sign, among others:

DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.'); 
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);

currentLocale can be obtained from Locale.getDefault() i.e.:

Locale currentLocale = Locale.getDefault();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to set space separator to float DecimalFormat?

DecimalFormat uses comma separator even when the pattern specified is a dot

How do I change the decimal separator in r-exams question to comma?

How to accept both dot and comma as a decimal separator with WTForms?

Format a decimal with comma as decimal separator and without thousands separator

How to convert floating point decimal separator from dot to comma in Javascript

How to write a csv with a comma as the decimal separator?

Replace comma with decimal separator from TextField

KSQL : How can I change separator (comma) of DELIMITED FORMAT?

decimal separator in woocommerce to be a comma

How to change the grouping separator (thousands) of DecimalFormat from comma/point to quote using patterns?

AngularJS: Change decimal separator to comma

How to format a float with a comma as decimal separator in an f-string?

How to get strings parsed to floats with comma as decimal separator

How to have float format with comma as a decimal separator in Python Pandas?

Replace point decimal separator with comma

Parsing numbers with a comma decimal separator in JavaScript

How to change decimal separator from comma to fullstop in redshift copy command

How to pass a decimal value from JSP to Action with a Localized decimal separator?

In PowerPoint, how do you replace a decimal separator with a comma

How to change VBA array decimal separator?

How to change decimal comma to decimal period in numpad?

Change decimal separator on Android

How do I change decimal dot into decimal comma?

How to change decimal separator in ExecuteReader c#

Pyspark, how to write a df with comma as a decimal separator

How to replace decimal separator dot by decimal separator comma in formattable() package R?

DecimalFormat("0.0") returns data with a comma as separator instead of a point

How to cast from String to BigDecimal with the separator as a comma?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive