Domanda

I have Double value of my variable is 8233.4892578125 and now when i am passing it to convert it as 8233.5 using below code which is not working as expected .

DecimalFormat df = new DecimalFormat("#.#");
    private String convertToFormat(double value){

        return df.format(value);
    }

Now when i pass convertToFormat(8233.4892578125) it give me below output :

8233,5

So why this happen it should give me 8233.5 not 8233,5 can any one give me proper answer for this issue.

NOTE : this issue happen when i have change my android emulator language to Italian from setting.

È stato utile?

Soluzione

it a Locale issue. You should supply a local that make uses of the point for the decimal representation. Use a european locale and you should get the point (Locale.ITALIAN e.g.).

Edit:

DecimalFormat decimalFormat = (DecimalFormat)
        NumberFormat.getNumberInstance(Locale.ITALIAN);
decimalFormat.applyPattern("#.#");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top