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,000for 100000010,000for 100001,000for 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 中使用逗号
基础教程推荐
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
