How to Set the Decimal Separator to Be a Comma

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

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();

How can I set the decimal separator to be a comma?

You should use basic_ios::imbue to set the preferred locale.

Take a look here: http://www.cplusplus.com/reference/ios/ios_base/imbue/

Locales allow you to use the preferred way by the user, so if a computer in Italy uses comma to separate decimal digits, in the US the dot is still used. Using locales is a Good Practice.

But if you want to explicitly force the use of the comma, take a look here:
http://www.cplusplus.com/reference/locale/numpunct/decimal_point/

Here a small example I just made with g++ which enforces the char ',' (passing the separator as template argument is just for fun, not really necessary)

#include <iostream>
#include <locale>

template <class charT, charT sep>
class punct_facet: public std::numpunct<charT> {
protected:
charT do_decimal_point() const { return sep; }
};

int main(int argc, char **argv) {
std::cout.imbue(std::locale(std::cout.getloc(), new punct_facet<char, ','>));
std::cout << "My age is " << 3.1415 << " lightyears.\n";
}

Note that using cout.getloc() I'm overriding just a single facet in the currently set locale, that is, in the current locale settings of cout, I'm changing only how the punctuation is done.

do_decimal_point is a virtual function of std::numpunct that you can redefine to provide your custom separator. This virtual function will be used by numpunct::decimal_point when printing your number.

AngularJS: Change decimal separator to comma

To change the separators used by the AngularJS number filter, include the appropriate locale rule set such as angular-locale_de-de.js.

There are two approaches to providing locale rules to AngularJS:

  • Pre-bundled rule sets
  • Including a locale script in index.html

For more information, see

  • AngularJS Developer Guide - Providing locale rules to AngularJS
  • AngularJS number Filter API Reference

The DEMO

<script src="//unpkg.com/angular/angular.js"></script>
<script src="//unpkg.com/angular-i18n/angular-locale_de-de.js"></script>
<body ng-app>
<span><b>5min=(5/60)=</b>
{{ 5/60 | number:2 }} hr (2 decimal places)
</span>
</body>

Use pandas style using comma as decimal separator

The most straightforward fix is to specify the decimal parameter of Styler.format:

tb.style.format(decimal=',').bar()

formatted with decimal as comma and bar

Other alternatives include setting the formatter to a function like locale.str

locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')

tb.style.format(formatter=locale.str).bar()

dataframe styled using locale string format and bar

Or locale.format_str:

locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')

tb.style.format(
formatter=lambda x: locale.format_string('%.6f', x)
).bar()

DataFrame formatted with 6 decimal precision and decimal point as comma with overlayed bar chart

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

You can (and you actually need to avoid localization) actively configure the DecimalFormat with more detail as follows:

public class Main {
public static void main(String[] args) throws Exception {
DecimalFormat df = new DecimalFormat("0.0");
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
decimalFormatSymbols.setDecimalSeparator('.');
df.setDecimalFormatSymbols(decimalFormatSymbols);

System.out.println(df.format(10.4)); // prints 10,4 instead of 10.4
System.out.println(df.format(100.5)); // prints 100,5 instead of 100.5
System.out.println(df.format(3000.3));// prints 3000,3 instead of 3000.3
}
}

You can read more details in the reference documentation, where an important snippet can be read:

Special Pattern Characters

(...)

The characters listed here are used
in non-localized patterns. Localized patterns use the corresponding
characters taken from this formatter's DecimalFormatSymbols object
instead, and these characters lose their special status.

Show comma instead of point as decimal separator

Well, as far as I know, there are no culture-specific options for convert available.

So you can do it using replaces (yes, it looks a bit ugly...)

select 
replace(replace(replace(convert(varchar(30), @euros, 1), ',', '|'), '.', ','), '|', '.')

Idea: first change comma to something, then change dot to comma, and then "something" back to dot.



Related Topics



Leave a reply



Submit