Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.format fails to format numbers on some devices?

Tags:

java

android

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:

٠product٠
٠product١
٠product٢
٠product٣
٠product٤

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

like image 863
Winter is coming Avatar asked Apr 14 '18 12:04

Winter is coming


2 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);
like image 60
Ralf Kleberhoff Avatar answered Nov 13 '22 15:11

Ralf Kleberhoff


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);
like image 42
Hitesh Sarsava Avatar answered Nov 13 '22 14:11

Hitesh Sarsava