1

I tried to get the local decimal separator with the following code:

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

//option 1
DecimalFormat format=(DecimalFormat) NumberFormat.getInstance();
DecimalFormatSymbols symbols=format.getDecimalFormatSymbols();
public char sep=symbols.getDecimalSeparator();

//option 2
double value = 1234.5;
NumberFormat fmt = NumberFormat.getInstance();
String formatted = fmt.format(value);
public String decSep= formatted.substring(5,6);

//option 3
public Locale loc=Locale.getDefault();
DecimalFormatSymbols symbols= new DecimalFormatSymbols(loc);
public char sep=symbols.getDecimalSeparator();
public String decSep=Character.toString(sep);

but I always get "," even if the setting is set to "." I tried with VB.NET code and it works. What's wrong or missing in Java code ? I appreciate your help

  • 2
    What's your default locale? And what do you mean with "even if the setting is set to"? – Marvin Aug 27 '15 at 01:19
  • Locale.getDefault() returns es_CO (is oK). I mean even if local setting is set to dot. – José Fernando Giraldo Jimenez Aug 27 '15 at 01:40
  • 1
    How exactly is it *set to dot*? In Java, the decimal mark for Spanish is ",", which - according to Wikipedia - seems to be correct for Spain as well as for Colombia. (https://en.wikipedia.org/wiki/Decimal_mark#Countries_using_Arabic_numerals_with_decimal_point) – Marvin Aug 27 '15 at 02:18
  • I think, this easily answered in the related [link] (http://stackoverflow.com/questions/4375410/recommended-way-to-format-numbers-in-a-locale-aware-way) – Ferdinand Neman Aug 27 '15 at 02:48
  • try to explain I need to know if a user has changed in your local settings decimal separator "," by "." . For example , I have changed, and Java can not detect the change while with VB .NET I can do it. How can I do that with Java? Thank you – José Fernando Giraldo Jimenez Aug 27 '15 at 02:55
  • Thank you Ferdinand but isn't work I tried: NumberFormat formatter = NumberFormat.getInstance(loc); String localeFormattedNumber = formatter.format(0.01); This returns 0,01, and should return 0.01, because in my local settings decimal separator is " . " – José Fernando Giraldo Jimenez Aug 27 '15 at 03:16
  • Are you talking about the locale preferences set in e.g. Windows region options? Then maybe this is related: http://stackoverflow.com/questions/6711925/how-can-i-so-date-and-time-formatting-in-java-that-respects-the-users-os-settin (hint: might not be easily possible with Java) – Marvin Aug 27 '15 at 09:39
  • Yes , that's what I try to do and I 've found at websites that is not possible with java . Thanks to all. – José Fernando Giraldo Jimenez Aug 28 '15 at 16:47

1 Answers1

1

Try this :

Locale.setDefault(Locale.US);

This will set every output number with decimal separator. You can also take some input numbers with decimal separator.

Kristiyan Duba
  • 51
  • 1
  • 1
  • 4