Home » Android » java – String.format fails to format numbers on some devices?

java – String.format fails to format numbers on some devices?

Posted by: admin June 15, 2020 Leave a comment


I have the following code:

int color = 0;
int number = 0;
// ...
s = String.format("%dproduct%d", color, number);

This works fine on my computer and on my Android device. I get strings like 0product0 etc.
BUT in production, on users’ Android devices, I get strings like:


Does String.format() not work OK when starting with a number?

How to&Answers:

The strings you get are “correct” for an Arabic Locale, as e.g. the UTF-8 dual-byte value d9 a0 (as posted by Andy) encodes the Unicode code point U+0660, being the ARABIC-INDIC DIGIT ZERO.

As the rest of your output isn’t locale-dependent, you probably want the numbers shown in US Locale as well, as proposed by Hitesh Sarsava:

String s = String.format(Locale.US, "%dproduct%d", color, number);


The variable here is the default Locale which may be different on devices.

To help explain, here is the hex view of the string output you posted:

d9 a0 70 72 6f 64 75 63 74 d9 a0  

which displays on my PC (UTF-8) as:


So follow advice in other answer and also continue researching “Locale”.

It may be helpful to know which region the other devices are not working to be more specific.

It is an interesting problem.

You can also attempt to recreate this on your own emulator by using the ‘Custom Locale’ app that is pre-installed on the emulator. I can’t say for certain the language you need is supported.

Based on @RalfKleberhoff ‘s excellent sleuthing, I was able to recreate this on an emulator using the ‘Custom Locale’ pre-installed app and change it to the ‘ar’ locale (first one on list). (Mainly for my own education – but may be useful for you as well.)

What’s even more enlightening(?) is if you programmed something like:


i.e. no formatting – it works in ‘ar’ locale as in displaying “en” numbers, so it seems the issue is limited to just the formatting (“%…”) aspect of this function – clear as mud.


use format like this :

String s = String.format(Locale.getDefault(), "%dproduct%d", color, number);

or use as per your requirement like this :

String s = String.format(Locale.US, "%dproduct%d", color, number);