How can I format a String number to have commas in android Edit Field(如何格式化字符串数字以在 android Edit Field 中使用逗号)
问题描述
我可以在android中使用什么功能将数字显示为不同的格式.
For what function I can use in android to display the number into different formats.
例如:如果我输入 1000,那么它应该显示为 1,000.如果我输入 10000,那么它应该显示为 10,000.如果我输入 1000000,那么它应该显示为 1,000,000.
For eg: If I enter 1000 then it should display like this 1,000. If I enter 10000 then it should display like this 10,000. If I enter 1000000 then it should display like this 1,000,000.
请指导我.
推荐答案
你可以使用 DecimalFormat
并且只是格式化数字
You could use DecimalFormat
and just format the number
DecimalFormat formatter = new DecimalFormat("#,###,###");
String yourFormattedString = formatter.format(100000);
结果将是
1,000,000
表示 100000010,000
为 100001,000
表示 1000
1,000,000
for 100000010,000
for 100001,000
for 1000
2019 年 12 月 2 日更新
这 String.format("%,d", number)
将是一个更好(更少硬编码)的解决方案,如@DreaminginCode 下面的评论中所示,所以我想我会在这里添加它另一种选择
This String.format("%,d", number)
would be a better(less hardcoded) solution as indicated in the comments below by @DreaminginCode so I thought I would add it here as an alternative
这篇关于如何格式化字符串数字以在 android Edit Field 中使用逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何格式化字符串数字以在 android Edit Field 中使用逗号


基础教程推荐
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 大摇大摆的枚举 2022-01-01