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 OverflowA 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!
- use
String.format - with
%,dwhich means a decimal with thousand separator - then pick a locale where the thousand separator is a
.like Germany:
println(String.format(Locale.GERMANY, "%,d", 1000000))
1.000.000
println(String.format(Locale.GERMANY, "%,d", 750000))
750.000
Even-though It's late. Intended for future visitors.
Fetures of the following codes
Puts thousand separator in
EditTextas it's text changes.adds
0.Automatically when pressed period (.) At First.Ignores
0input 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
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
}