Search code examples
kotlinnumbersnumber-formatting

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

Solution

  • 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?