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 中使用逗号


- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01