Why does Android Lint warn about String.format using default locale when explicitly using Locale.US?

AndroidEclipseAndroid Lint

Android Problem Overview


I originally called String.format this way:

return String.format("%s %f %f", anotherString, doubleA, doubleB);

Which made Android Lint generate this warning:

> Implicitly using the default locale is a common source of bugs: Use String.format(Locale, ...) instead

So I changed it to use Locale.US explicitly, based on what I read at http://developer.android.com/reference/java/util/Locale.html under the "Be wary of the default locale" section:

return String.format(Locale.US, "%s %f %f", anotherString, doubleA, doubleB);

Why does Android Lint still generate the same warning? I have to clean the project in Eclipse to get rid of it, when most warnings just disappear as soon as the offending line is fixed. I'm not sure if I'm doing something wrong or not.

Android Solutions


Solution 1 - Android

Cleaning and rebuilding the project should work.

BTW, you may want to use Locale.getDefault() to "take care" of texts not written in english.

Solution 2 - Android

when I mentioned the locale with the format, the lint warning just vanished.

String.format(Locale.US,"%02d", selectedInt);

Solution 3 - Android

Implied default locale in case conversion

Calling String#toLowerCase() or #toUpperCase() without specifying an explicit locale is a common source of bugs. The reason for that is that those methods will use the current locale on the user's device, and even though the code appears to work correctly when you are developing the app, it will fail in some locales. For example, in the Turkish locale, the uppercase replacement for i is not I.

If you want the methods to just perform ASCII replacement, for example to convert an enum name, call String#toUpperCase(Locale.US) instead. If you really want to use the current locale, call String#toUpperCase(Locale.getDefault()) instead.

http://developer.android.com/reference/java/util/Locale.html#default_locale

Solution 4 - Android

Simply add Your Locale; for English Locale,

return String.format(Locale.ENGLISH,"%s %f %f", anotherString, doubleA, doubleB);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNobody SpecialView Question on Stackoverflow
Solution 1 - AndroidAlejandro ColoradoView Answer on Stackoverflow
Solution 2 - AndroidPratheeshView Answer on Stackoverflow
Solution 3 - AndroidPaulo MendonçaView Answer on Stackoverflow
Solution 4 - AndroidW RobertsView Answer on Stackoverflow