I want to add commas or point every 3 digit in EditText input.
Example :
To do it without using NumberFormat , you can convert the number to a String and do the following code: double number = 1526374856.03; String[] array = Double. toString(number).
The joinToString() function is used to convert an array or a list to a string which is separated with the mentioned separator. In the example above, I am using joinToString() to convert the list of Kotlin data classes into a comma separated string. Notice it -> “\'${it. nameOfStringVariable}\'” ?
A comma is placed every third digit to the left of the decimal point and so is used in numbers with four or more digits. Continue to place a comma after every third digit. For example: $1,000,000 (one million dollars)
If you are on the JVM you can use
"%,d".format(input)
which gives 11,000
for input 11000
. Replace ,
with any delimiter you require.
If you want to use predefined number formats, e.g. for the current locale, use:
java.text.NumberFormat.getIntegerInstance().format(input);
Be also sure to check the other format instances, e.g. getCurrencyInstance
or getPercentInstance
. Note that you can use NumberFormat
also with other locales. Just pass them to the get*Instance
-method.
Some of the second variant can also be found here: Converting Integer to String with comma for thousands
If you are using it via Javascript you may be interested in: How do I format numbers using JavaScript?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With