简体   繁体   English

如何格式化字符串数字以在 android 编辑字段中包含逗号

[英]How can I format a String number to have commas in android Edit Field

For what function I can use in android to display the number into different formats.对于什么 function 我可以在 android 中使用将数字显示为不同的格式。

For eg: If I enter 1000 then it should display like this 1,000.例如:如果我输入 1000,那么它应该显示为 1,000。 If I enter 10000 then it should display like this 10,000.如果我输入 10000,那么它应该显示为 10,000。 If I enter 1000000 then it should display like this 1,000,000.如果我输入 1000000,那么它应该显示为 1,000,000。

Please guide me.请指导我。

For what function I can use in android to display the number into different formats.我可以在android中使用什么功能将数字显示为不同的格式。

For eg: If I enter 1000 then it should display like this 1,000.例如:如果我输入1000,则它应显示为1000。 If I enter 10000 then it should display like this 10,000.如果输入10000,则应显示为10,000。 If I enter 1000000 then it should display like this 1,000,000.如果我输入1000000,则它应显示为1,000,000。

Please guide me.请指导我。

For what function I can use in android to display the number into different formats.我可以在android中使用什么功能将数字显示为不同的格式。

For eg: If I enter 1000 then it should display like this 1,000.例如:如果我输入1000,则它应显示为1000。 If I enter 10000 then it should display like this 10,000.如果输入10000,则应显示为10,000。 If I enter 1000000 then it should display like this 1,000,000.如果我输入1000000,则它应显示为1,000,000。

Please guide me.请指导我。

For what function I can use in android to display the number into different formats.我可以在android中使用什么功能将数字显示为不同的格式。

For eg: If I enter 1000 then it should display like this 1,000.例如:如果我输入1000,则它应显示为1000。 If I enter 10000 then it should display like this 10,000.如果输入10000,则应显示为10,000。 If I enter 1000000 then it should display like this 1,000,000.如果我输入1000000,则它应显示为1,000,000。

Please guide me.请指导我。

For what function I can use in android to display the number into different formats.我可以在android中使用什么功能将数字显示为不同的格式。

For eg: If I enter 1000 then it should display like this 1,000.例如:如果我输入1000,则它应显示为1000。 If I enter 10000 then it should display like this 10,000.如果输入10000,则应显示为10,000。 If I enter 1000000 then it should display like this 1,000,000.如果我输入1000000,则它应显示为1,000,000。

Please guide me.请指导我。

For what function I can use in android to display the number into different formats.我可以在android中使用什么功能将数字显示为不同的格式。

For eg: If I enter 1000 then it should display like this 1,000.例如:如果我输入1000,则它应显示为1000。 If I enter 10000 then it should display like this 10,000.如果输入10000,则应显示为10,000。 If I enter 1000000 then it should display like this 1,000,000.如果我输入1000000,则它应显示为1,000,000。

Please guide me.请指导我。

For what function I can use in android to display the number into different formats.我可以在android中使用什么功能将数字显示为不同的格式。

For eg: If I enter 1000 then it should display like this 1,000.例如:如果我输入1000,则它应显示为1000。 If I enter 10000 then it should display like this 10,000.如果输入10000,则应显示为10,000。 If I enter 1000000 then it should display like this 1,000,000.如果我输入1000000,则它应显示为1,000,000。

Please guide me.请指导我。

For what function I can use in android to display the number into different formats.我可以在android中使用什么功能将数字显示为不同的格式。

For eg: If I enter 1000 then it should display like this 1,000.例如:如果我输入1000,则它应显示为1000。 If I enter 10000 then it should display like this 10,000.如果输入10000,则应显示为10,000。 If I enter 1000000 then it should display like this 1,000,000.如果我输入1000000,则它应显示为1,000,000。

Please guide me.请指导我。

For what function I can use in android to display the number into different formats.我可以在android中使用什么功能将数字显示为不同的格式。

For eg: If I enter 1000 then it should display like this 1,000.例如:如果我输入1000,则它应显示为1000。 If I enter 10000 then it should display like this 10,000.如果输入10000,则应显示为10,000。 If I enter 1000000 then it should display like this 1,000,000.如果我输入1000000,则它应显示为1,000,000。

Please guide me.请指导我。

I wrote this Kotlin extension function may can help.我写的这个 Kotlin 扩展函数可能会有所帮助。

fun String.formatPoint(): String {
    val sb = StringBuilder()
    this.reversed().forEachIndexed { index, c ->
        // 123,123,123
        if ((index + 1) % 3 == 0) {
            if (index != this.length - 1) {
                sb.append("$c,")
            } else {
                sb.append(c)
            }
        } else {
            sb.append(c)
        }
    }

    return sb.toString().reversed()
}

Recommended and preferred way is to do it with the strings.xml file推荐和首选的方法是使用strings.xml文件

<string name="format_denominated">%,d</string>

from your Kotlin/Java code来自你的 Kotlin/Java 代码

getResources().getString(R.string.format_denominated, value)

Even better with databinding更好的数据绑定

<TextView
    android:text="@{@string/format_denominated(value)}"
    ............/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM