問題描述
我可以在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 中使用逗號的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!