猿问

我如何单独格式化双值逗号

我目前正在 Spring MVC 中开发工资支付模块,在应用程序级别,我将我的工资数据存储在双类型中, eg double amount=32690597

我想把它格式化成 32,690,597,这样它就更易读了。

建议我最好的解决方案,谢谢


梦里花落0921
浏览 159回答 3
3回答

蓝山帝景

System.out.println(String.format("%1$,.0f", amount));或者String formatAmount = new DecimalFormat("#,###,###,###,###").format(amount);或 - Java 8NumberFormat numberFormatter = NumberFormat.getNumberInstance(Locale.ENGLISH);String formatAmount = numberFormatter.format(amount);

慕运维8079593

使用DecimalFormat这个问题。double amount = 32690597;//create pattern for decimal formatString pattern = "###,###";DecimalFormat decimalFormat = new DecimalFormat(pattern);//change dot with commaString formattedNumber = decimalFormat.format(amount).replace(".",",");详细十进制格式键;0   A digit - always displayed, even if number has less digits (then 0 is displayed)#   A digit, leading zeroes are omitted..   Marks decimal separator,   Marks grouping separator (e.g. thousand separator)E   Marks separation of mantissa and exponent for exponential formats.;   Separates formats-   Marks the negative number prefix%   Multiplies by 100 and shows number as percentage?   Multiplies by 1000 and shows number as per mille¤   Currency sign - replaced by the currency sign for the Locale. Also makes formatting use the monetary decimal separator instead of normal decimal separator. ¤¤ makes formatting use international monetary symbols.X   Marks a character to be used in number prefix or suffix'   Marks a quote around special characters in prefix or suffix of formatted number.一些与此相关的示例;Pattern      Number        Formatted String###.###      123.456       123.456###.#        123.456       123.5###,###.##   123456.789    123,456.79000.###      9.95          009.95##0.###      0.95          0.95

达令说

static String formatDouble(double input){    String inputString = new String(Double.toString(input));    char[] array= inputString.toCharArray();    StringBuilder str = new StringBuilder("");    for(int i = array.length-1; i >= 0; i--){        str.insert(0, array[i]);            if(i % 3 == 0 && i != 0){            str.insert(0, ",");            }        }    return str.toString();    }希望这可以帮助更好地使用 String.format
随时随地看视频慕课网APP

相关分类

Java
我要回答