Decimal Format In Android

Yesterday, I found a bug in the app that I working on. If it’s the Indonesian language on phone, my app can’t work properly. Because Android didn’t return the decimal if we are using the Indonesia language setting.

Example :

String amt = String.format("%.2f",20.05);
Log.i("test","Amount is " + amt); //if indo language , it will return 20,00 instead of 20.05

I tested with DecimalFormat but the same result.

DecimalFormat f = new DecimalFormat("##.00");
String atm = f.format(d); 
Log.i("test","Amount is " + amt); //if indo language , it will return 20,00 instead of 20.05

So, I am using like following for now.

String amt = String.format("%.2f",20.05);

if(atm.indexOf(",") != -1)
{
    atm = atm.replace(",", ".");
}

Log.i("test","Amount is " + amt);
Gravatar Image
My name is Than Dana but I’d like to be called Dana. I’m 28 years old and a single man. I’m from Toungoo, Bago division. I live in Yangon for my job and work as Digital Marketing Executive @ Win Mobile World Co., Ltd. I stand as a professional in IT fields and have been sharing, inventing and advancing tech developments since about 2008.