在整数中插入逗号

发布于 2024-10-03 09:46:48 字数 251 浏览 0 评论 0原文

在 Android 中,有没有一种简单的方法可以将逗号插入数字值? 即,如果我有一个包含 12345 的 EditText,如何将其显示为 12,345?

我想我可以使用子字符串并将其切成 x 个 3 数字块,然后在每个 3 数字块之间用“,”连接答案。 如果数字的长度是恒定的,这将非常容易,但由于数字可能从一位数字到 20,这使得它变得更加复杂。

只是好奇在我开始制作子字符串解决方案之前是否有一种简单而干净的方法来实现这一目标。

干杯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的故事 2024-10-10 09:46:48

如果 Java --

使用类似这样的内容:

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

结果:十进制值为:2,324,343,192.02

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

以为你会在 2024-10-10 09:46:48

你可以首先声明下面的类:

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);
}

}

然后在 xml 文件中声明:

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

并在主活动中:

TextView txt = findViewById(R.id.txt);
txt.addTextChangedListener(new MyNumberWatcher(txt));

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文