Dual Android formatting

I want to turn, for example. 5999.635554 - 5,999.63. I used the DecimalFormat method below:

 public static String decimalDoubleForm(double value){ DecimalFormat df = new DecimalFormat("#,###,###.00"); String formatted_value = df.format(value).replaceAll("\\.", ","); return formatted_value; } 

However, when I used it, i.e. Util.decimalDoubleForm(value); e.g. -4000.4343 I get the following -4,000,43.

my desired result should be -4000.4343 => -4 000.43.

+4
source share
1 answer

If you want -4,000.43 (the line from the format() call) to become -4 000,43 , you can make two replacements.

First, replace all characters , spaces, and then replace all . characters on,.

 String formatted_value = df.format(value) .replaceAll("," , " ") .replaceAll("\\.", ","); 

This becomes problematic if you want to exchange , and . (a) but you can still do this, albeit through a three-step process:

 comma => X period => comma X => period 

(where X is any character that otherwise does not appear on the string, for example, "X" ).

However, there may be a format string that you can use to do this without having to replace the strings. From here , it seems that special characters are localized, so there should be a way to create a DecimalFormat with a specific DecimalFormatSymbol based on the locale.

At least the fact is that in Java itself. Can Java Android do the same, I'm not sure. It is known that they used to take a different approach to internationalization.


(a) Trick these Europeans and their ridiculous number formats :-)

+2
source

Source: https://habr.com/ru/post/1482897/


All Articles