The code "%.2f".format(1.221)
returns ۱,۲۲
instead of 1.22
on Persian/Arabic/Urdu language devices. How can I make it return always "English" numerals?
I tried "%.2f".format(1.221, Locale.ENGLISH)
but it still does not work.
You have the right idea but applying it incorrectly. You indeed simply update the locale. One way is to use the Formatter
class:
import java.util.Formatter;
import java.util.Locale;
void main() {
var fmt = new Formatter(Locale.ENGLISH);
System.out.println(fmt.format("%.2f", 1.221));
}
UPDATE: I missed some overloads; PrintStream
's printf
has a Locale variant, as does String.format
. Thus, this would also work:
import java.util.Locale;
void main() {
System.out.printf(Locale.ENGLISH, "%.2f\n", 1.221);
String example = String.format(Locale.ENGLISH, "%.2f\n", 1.221);
}