Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add commas or point every 3 digits using kotlin

I want to add commas or point every 3 digit in EditText input.

Example :

  • input : 1000. Output : 1.000
  • input : 11000. Output : 11.000
like image 527
Ahmed Avatar asked Nov 08 '18 11:11

Ahmed


People also ask

How do you add a comma every three digits without using the number format?

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).

How does kotlin get Comma Separated Values?

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}\'” ?

How do you add commas to numbers?

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)


1 Answers

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?

like image 170
Roland Avatar answered Oct 20 '22 11:10

Roland