Home > Mobile >  How to format number in "English" numerals only?
How to format number in "English" numerals only?

Time:10-23

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.

CodePudding user response:

You have the right idea but applying it incorrectly. You indeed simply update the locale. However, String.format is syntax sugar, and oversimplified - it can't do that. You have to do what String.format does under the hood use the Formatter class:

import java.util.Formatter;

void main() {
  var fmt = new Formatter(Locale.ENGLISH);
  println(fmt.format("%.2f", 1.221));
}
  • Related