androidcurrencynumberformatter

Remove trailing zeros from currency string android


I tried to convert int to curency format and everything worked as I expected

example 1000 --> Rp1.000

but on certain devices the results are not what I expected
i.e. 1000 --> Rp1.000,00

So the question is how to remove the last 2 extra nolls if the device displays them.

my current code:

    public static String toUang(String DigitUang) {

        return NumberFormat.getCurrencyInstance(new Locale("in", "ID")).format(Double.parseDouble(DigitUang));
    }

Solution

  • The best solution here would probably be to fix your locale settings and handle the presentation there. That being said, you also could probably handle this using a regex replacement:

    public static String toUang(String DigitUang) {
    
        return NumberFormat.getCurrencyInstance(new Locale("in", "ID"))
                           .format(Double.parseDouble(DigitUang))
                           .replaceAll("[,.]00$", "");
    }