Issue
I want to format a number in Android
(or Java
for that matter) say
12345.67
to
12,345.67
But that it will work with lower API level, namely, from API level 15 and up.
I tried NumberFormat NumberFormat.getInstance().format(myNumber)
or NumberFormat.getCurrencyInstance().format(myNumber)
to no avail. Also I tried DecimalFormat without success also, that is, it will give you a warning Call requires API level 24 (current min is 15)
which basically means that although my code will work with phone running on API level 24 but not with older version of Android that uses at least API level 15.
So, what would be my recourse? I don't want to use by the way a utility function just for this but preferably a method from a library. Also System.out.format()
is out of the question, since I want to show it using Toast
.
Solution
Wow, I thought it was something that I have to make a special utility function.
It was simply a String.format()
method did the trick.
Double num = 12345.67;
String formatted = String.format("%,.2f", num);
Answered By - Edper
Answer Checked By - David Goodson (JavaFixing Volunteer)