Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 708
Nobody Special Avatar asked Jun 04 '13 21:06

Nobody Special


People also ask

What is locale in string format?

locale which is the locale value to be applied on the this method. format which is the format according to which the String is to be formatted. args which is the number of arguments for the formatted string. It can be optional, i.e. no arguments or any number of arguments according to the format.

What does locale default mean?

The default Locale is constructed statically at runtime for your application process from the system property settings, so it will represent the Locale selected on that device when the application was launched.


3 Answers

Cleaning and rebuilding the project should work.

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

like image 52
Alejandro Colorado Avatar answered Oct 06 '22 23:10

Alejandro Colorado


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

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

like image 16
Pratheesh Avatar answered Oct 07 '22 00:10

Pratheesh


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

like image 11
Paulo Mendonça Avatar answered Oct 07 '22 01:10

Paulo Mendonça