A simple way to do this would be using java.text.DecimalFormat:

fun formatter(n: Int) =
    DecimalFormat("#,###")
        .format(n)
        .replace(",", ".")

println(formatter(1000000)) // => 1.000.000
println(formatter(750000))  // => 750.000

However, do note that the dot (.) symbol is commonly used as a Decimal separator (or monetary decimal separator)

So I would advice using , instead of of ..

Try it online!


Update:

If your intention is having a German locale (Thanks @AndrewL) formatting, then configure the DecimalFormatSymbols with the correct locale:

fun formatter(n: Int) =
    DecimalFormat("#,###", DecimalFormatSymbols(Locale.GERMANY)).format(n)

Try it online!

Answer from u-ways on Stack Overflow
🌐
Baeldung
baeldung.com › home › kotlin › kotlin numbers › format number with thousand separator in kotlin
Format Number With Thousand Separator in Kotlin | Baeldung on Kotlin
February 22, 2025 - The “#,###” pattern will format our number to a thousand-separated String, but with a comma separator.
🌐
Baeldung
baeldung.com › home › kotlin › kotlin strings › parse string with thousands separator in kotlin
Parse String With Thousands Separator in Kotlin | Baeldung on Kotlin
May 4, 2024 - First, we create an instance of StringTokenizer with the separator character as delimiter. Then, we loop through each token in the string and append it to a StringBuilder. Finally, we convert the number to an Int.
🌐
Kotlin Discussions
discuss.kotlinlang.org › android
"%.#f".format function return value with "comma" separator, I need it with "dot" separator - Android - Kotlin Discussions
November 21, 2017 - Hi Kotlin community. I am using “%.#”.format(number) to use only # decimal numbers to avoid display all decimal values, but this function returns with “comma” separator and I need this values with “dot” separator. I ha…
Top answer
1 of 16
62

Even-though It's late. Intended for future visitors.

Fetures of the following codes

  1. Puts thousand separator in EditText as it's text changes.

  2. adds 0. Automatically when pressed period (.) At First.

  3. Ignores 0 input at Beginning.

Just copy the following Class named

NumberTextWatcherForThousand which implements TextWatcher

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import java.util.StringTokenizer;

/**
 * Created by skb on 12/14/2015.
 */
public class NumberTextWatcherForThousand implements TextWatcher {

    EditText editText;


    public NumberTextWatcherForThousand(EditText editText) {
        this.editText = editText;


    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        try
        {
            editText.removeTextChangedListener(this);
            String value = editText.getText().toString();


            if (value != null && !value.equals(""))
            {

                if(value.startsWith(".")){
                    editText.setText("0.");
                }
                if(value.startsWith("0") && !value.startsWith("0.")){
                    editText.setText("");

                }


                String str = editText.getText().toString().replaceAll(",", "");
                if (!value.equals(""))
                editText.setText(getDecimalFormattedString(str));
                editText.setSelection(editText.getText().toString().length());
            }
            editText.addTextChangedListener(this);
            return;
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            editText.addTextChangedListener(this);
        }

    }

    public static String getDecimalFormattedString(String value)
    {
        StringTokenizer lst = new StringTokenizer(value, ".");
        String str1 = value;
        String str2 = "";
        if (lst.countTokens() > 1)
        {
            str1 = lst.nextToken();
            str2 = lst.nextToken();
        }
        String str3 = "";
        int i = 0;
        int j = -1 + str1.length();
        if (str1.charAt( -1 + str1.length()) == '.')
        {
            j--;
            str3 = ".";
        }
        for (int k = j;; k--)
        {
            if (k < 0)
            {
                if (str2.length() > 0)
                    str3 = str3 + "." + str2;
                return str3;
            }
            if (i == 3)
            {
                str3 = "," + str3;
                i = 0;
            }
            str3 = str1.charAt(k) + str3;
            i++;
        }

    }

    public static String trimCommaOfString(String string) {
//        String returnString;
        if(string.contains(",")){
            return string.replace(",","");}
        else {
            return string;
        }

    }
}

Use This Class on your EditText as follows

editText.addTextChangedListener(new NumberTextWatcherForThousand(editText));

To get the input as plain Double Text

Use the trimCommaOfString method of the same class like this

NumberTextWatcherForThousand.trimCommaOfString(editText.getText().toString())

Git

2 of 16
57

You can use String.format() in a TextWatcher. The comma in the format specifier does the trick.

This does not work for floating point input. And be careful not to set an infinite loop with the TextWatcher.

public void afterTextChanged(Editable view) {
    String s = null;
    try {
        // The comma in the format specifier does the trick
        s = String.format("%,d", Long.parseLong(view.toString()));
    } catch (NumberFormatException e) {
    }
    // Set s back to the view after temporarily removing the text change listener
}
🌐
Android Developers
developer.android.com › api reference › numberformat
NumberFormat | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
GitHub
github.com › shreekrishnaban › NumberTextWatcherForThousand
GitHub - shreekrishnaban/NumberTextWatcherForThousand: This class is used For thousand seperator to the android EditText while taking currency input , This seperats the editText input with comma while user is entering the value
This class is used For thousand seperator to the android EditText while taking currency input , This seperats the editText input with comma while user is entering the value - shreekrishnaban/Number...
Starred by 22 users
Forked by 9 users
🌐
Android Developers
developer.android.com › api reference › decimalformat
DecimalFormat | API reference | Android Developers
June 8, 2022 - Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › Decimal_separator
Decimal separator - Wikipedia
1 week ago - The code shown below, written in Kotlin, illustrates the use of separators to increase readability: ... The International Bureau of Weights and Measures states that "when there are only four digits before or after the decimal marker, it is customary not to use a space to isolate a single digit." Likewise, some manuals of style state that thousands separators should not be used in normal text for numbers from 1000 to 9999 where no decimal fractional part is shown (or, in other words, for four-digit whole numbers), whereas others use thousands separators and others use both.
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
How to call Number.toLocaleString() in Kotlin/js? - Support - Kotlin Discussions
November 15, 2022 - Hello, I am currently experimenting with Kotlin/js and I wanted to add thousand separators to some numbers. The best way seems to be to use JavaScript’s Number.toLocaleString() method. I have not found a way to call it directly so I used the “js(…)” method to do so.
🌐
Commonsware
klassbook.commonsware.com › lessons › Basic Types › thousands-separator.html
CommonsWare Klassbook: Using Underscores for Thousands Separators
In this code, 1_234 is also a Kotlin Int type, representing an integer. _ can be used as a thousands separator in a number, to help make it a bit more readable.
🌐
Phrase
phrase.com › home › resources › blog › how do i convert a decimal to a string with thousands separators?
How Do I Convert a Decimal to a String with Thousands Separators?
January 23, 2025 - We have a number as a double or ... we need to take care of thousands separators; so 1000000 is formatted to the string "1,000,000", in the US English (en-US) locale, for example....
🌐
Kotlinlang
slack-chats.kotlinlang.org › t › 13396797 › hi-guys-i-need-to-show-thousands-separator-and-decimal-separ
Hi guys I need to show thousands separator and decimal separ kotlinlang #android
June 20, 2021 - Hi guys I need to show thousands separator and decimal separator of a number input by user currently I m using DecimalFormat and it works well if I specific a pattern ex But for me as a user when I in
🌐
GitHub
github.com › adibfara › Compose-TextField-Thousands-Separator
GitHub - adibfara/Compose-TextField-Thousands-Separator: Thousands-Separator implemented for Jetpack Compose's TextFields using a visual transformation
Note: You can have your own implementation of a thousand separator (based on locale or ...) and pass it as thousandSeparator argument.
Author   adibfara
🌐
Medium
medium.com › @surhid.amatya › jointostring-kotlin-function-to-get-comma-separated-strings-1b98a6939399
joinToString() Kotlin function to get comma separated strings | by Surhid Amatya | Medium
November 12, 2019 - Here I am going to display how I used to do and how Kotlin made the task easier ... List<String> serialNumbers = new ArrayList();for (int i=0; i< serialNumbers.size(); i++) {VehicleSerialNumber vehicleSerialNumber = serialNumbers.get(i);StringBuilder separatedSerialNumber = new StringBuilder();separatedSerialNumber.append(“\’”+vehicleSerialNumber.nameOfStringVariable +”\’”);if (i != listOfStringColumn.size() -1 ) {separatedSerialNumber.append(“, “);}}