我想在EditText输入中每隔3位添加逗号或点.

Example :

  • 输入:1000.输出:1.000
  • 输入:11000.输出:11.000

推荐答案

如果你在JVM上,你可以使用

"%,d".format(input)

输入11000等于11,000.将,替换为所需的任何分隔符.

If you want to use predefined number formats, e.g. for the current locale, use:

java.text.NumberFormat.getIntegerInstance().format(input);

Be also sure to check the other format instances, e.g. getCurrencyInstance or getPercentInstance. Note that you can use NumberFormat also with other locales. Just pass them to the get*Instance-method.

Some of the second variant can also be found here: Converting Integer to String with comma for thousands

If you are using it via Javascript you may be interested in: How do I format numbers using JavaScript?

Kotlin相关问答推荐

来自SnapshotFlow的单元测试StateFlow仅发出initialValue

如何在Kotlin中为两个数据类创建可重用的方法?

Scala性状线性化等价于Kotlin

Kotlin 说不需要强制转换,但删除后会出现新警告

如何将 `when` 与 2 个密封类一起使用并获取内部值?

ActivityResultContracts TakePicture 结果总是返回 false

返回 kotlin 中的标签和 lambda 表达式

T except one class

SpringBoot 2.5.0 对于 Jackson Kotlin 类的支持,请在类路径中添加com.fasterxml.jackson.module: jackson-module-kotlin

Kotlin 中多个 init 块的用例?

使用 Hilt 注入 CoroutineWorker

Kotlinwhen表达式在使用主题时是否支持复合布尔表达式?

变量后的Android问号

Kotlin内联扩展属性

使用范围的稀疏sparse值列表

如何在Kotlin中创建无限长的序列

在 Kotlin 函数上使用 Mokito anyObject() 时,指定为非 null 的参数为 null

如何根据ArrayList的对象属性值从中获取最小/最大值?

有没有办法在Kotlin中设置一个私有常量

如何在 Kotlin 中按字母顺序对字符串进行排序