5

In Android is there an easy way to insert commas into a numberical value? ie if I have an EditText with 12345 in it, how can I display this as 12,345?

I think I can do it using substring and chopping it up into x number of 3 number chunks then concatenating the answer with ',' between each 3 number chunk. This would be pretty easy if length of number was constant, but as the number could be from one digit to, say 20, it makes it more complex.

Just curious if there is a simple and clean way to achieve this before I go about making my substring solution.

Cheers

Entropy1024
  • 7,847
  • 9
  • 30
  • 32
  • you should probably specify what language you're using, no? – aaronasterling Nov 18 '10 at 22:19
  • Is this Java? (I'm guessing from "EditText") – Michael Mrozek Nov 18 '10 at 22:20
  • What language are you using? Many languages have built-in formatting functions. – Ray Nov 18 '10 at 22:20
  • Is this back-end code, or do you want a UI widget that does this for you (and if so, which UI library are you using)? Do you care about internationalization (e.g. using spaces instead of commas if users' machines are set to German; using groups of other-than-3 if that's appropriate to the user's language settings)? – Joe White Nov 18 '10 at 22:24
  • Yes sorry it is Android. – Entropy1024 Nov 18 '10 at 23:30
  • Here's a link to similar thread [http://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number](http://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number) – J Benjamin Nov 18 '10 at 22:25

2 Answers2

16

If Java --

Use something like this:

    double amount = 2324343192.015;
    NumberFormat formatter = new DecimalFormat("###,###,###.##");
    System.out.println("The Decimal Value is: "+formatter.format(amount));

Result: The Decimal Value is: 2,324,343,192.02

Griff
  • 1,796
  • 3
  • 23
  • 48
  • I have used the code below but it returns 123456.0, not as I would have expected 123,456. Where am I going wrong please? double amount = 123456; NumberFormat formatter = new DecimalFormat("###,###,###"); formatter.format(amount); totalFrames.setText(Double.toString(amount)); – Entropy1024 Nov 19 '10 at 00:29
  • Ah, Just figured out where I was going wrong. The returned formatted result is a string yes? – Entropy1024 Nov 19 '10 at 00:45
  • Yes, it is a string. There's no such thing a formatted number in Java. – Griff Nov 19 '10 at 16:02
0

you can first declare below class:

public class MyNumberWatcher implements TextWatcher {

private EditText editText;

public MyNumberWatcher(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) {
    editText.removeTextChangedListener(this);

    String sEditText = editText.getText().toString();

    sEditText = sEditText.replace(",", "");
    if (sEditText.length() > 0) {
        DecimalFormat sdd = new DecimalFormat("#,###");
        Double doubleNumber = Double.parseDouble(sEditText);

        String format = sdd.format(doubleNumber);
        editText.setText(format);
        editText.setSelection(format.length());

    }
    editText.addTextChangedListener(this);
}

}

then in xml file:

<EditText
                android:id="@+id/txt"
                style="@style/edit_text_style"
                android:inputType="number"/>

and in main activity:

TextView txt = findViewById(R.id.txt);
txt.addTextChangedListener(new MyNumberWatcher(txt));
M Karimi
  • 1,991
  • 1
  • 17
  • 34