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 中使用逗号
基础教程推荐
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 Windows 证书导入 Java 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
