如何格式化字符串数字以在 android Edit Field 中使用逗号 [英] How can I format a String number to have commas in android Edit Field

查看:24
本文介绍了如何格式化字符串数字以在 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 表示 1000000
  • 10,000 为 10000
  • 1,000 表示 1000
  • 1,000,000 for 1000000
  • 10,000 for 10000
  • 1,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 中使用逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆