Android API level 15 and above formatted currency or double
I want to format a number in Android
(or Java
).
12345.67
to
12,345.67
But it applies to lower API levels, API level 15 and higher.
I tried NumberFormat NumberFormat.getInstance().format(myNumber)
or NumberFormat.getCurrencyInstance().format(myNumber)
and it didn’t work .
I’ve also tried DecimalFormat without success, that is, it will give you a warning Call requires API level 24 (current min is 15) which basically means that while my code will work on phones running at API level 24, it won’t work on older versions of Android that use at least API level 15
.
So, what can I do? I don’t want to use utility functions by the way, but it’s better to use methods from the library. Also System.out.format()
is not considered because I want to use a toast
to display it.
Solution
Wow, I thought I had to do a special utility function.
This is just a String.format() method.
Double num = 12345.67;
String formatted = String.format("%,.2f", num);