1. 格式化
符号 | 描述 |
---|---|
0 | 0表示数字,如果位数不足则补 0 |
# |
#表示数组,小数超出部分四舍五入,位数不足则不显示对应的位置 |
. |
小数分隔符 |
, |
分组分隔符 |
E | 科学计数法 |
% |
百分号 |
‰ |
千分号 |
1.2 DecimalFormat 使用
@Testpublic void format() {BigDecimal value = new BigDecimal("1.35");System.out.println("0.0" + " ---> " + new DecimalFormat("0.0").format(value));System.out.println("#.#" + " ---> " + new DecimalFormat("#.#").format(value));System.out.println("0.000" + " ---> " + new DecimalFormat("0.000").format(value));System.out.println("#.###" + " ---> " + new DecimalFormat("#.###").format(value));System.out.println("0.0##" + " ---> " + new DecimalFormat("0.0##").format(value));System.out.println("#.#%" + " ---> " + new DecimalFormat("#.#%").format(value));}@Test public void format() { BigDecimal value = new BigDecimal("1.35"); System.out.println("0.0" + " ---> " + new DecimalFormat("0.0").format(value)); System.out.println("#.#" + " ---> " + new DecimalFormat("#.#").format(value)); System.out.println("0.000" + " ---> " + new DecimalFormat("0.000").format(value)); System.out.println("#.###" + " ---> " + new DecimalFormat("#.###").format(value)); System.out.println("0.0##" + " ---> " + new DecimalFormat("0.0##").format(value)); System.out.println("#.#%" + " ---> " + new DecimalFormat("#.#%").format(value)); }@Test public void format() { BigDecimal value = new BigDecimal("1.35"); System.out.println("0.0" + " ---> " + new DecimalFormat("0.0").format(value)); System.out.println("#.#" + " ---> " + new DecimalFormat("#.#").format(value)); System.out.println("0.000" + " ---> " + new DecimalFormat("0.000").format(value)); System.out.println("#.###" + " ---> " + new DecimalFormat("#.###").format(value)); System.out.println("0.0##" + " ---> " + new DecimalFormat("0.0##").format(value)); System.out.println("#.#%" + " ---> " + new DecimalFormat("#.#%").format(value)); }
打印结果:
0.0 ---> 1.4#.# ---> 1.40.000 ---> 1.350#.### ---> 1.350.0## ---> 1.35#.#% ---> 135%0.0 ---> 1.4 #.# ---> 1.4 0.000 ---> 1.350 #.### ---> 1.35 0.0## ---> 1.35 #.#% ---> 135%0.0 ---> 1.4 #.# ---> 1.4 0.000 ---> 1.350 #.### ---> 1.35 0.0## ---> 1.35 #.#% ---> 135%
2. 千分位格式化
@Testpublic void formatDemo () {double number = 123456789.345D;// 每三位数加一个逗号DecimalFormat df = new DecimalFormat("#,###.###");System.out.println("千分位: " + df.format(number));DecimalFormat df2 = new DecimalFormat("#,###.00");System.out.println("保留两位小数: " + df2.format(number));}@Test public void formatDemo () { double number = 123456789.345D; // 每三位数加一个逗号 DecimalFormat df = new DecimalFormat("#,###.###"); System.out.println("千分位: " + df.format(number)); DecimalFormat df2 = new DecimalFormat("#,###.00"); System.out.println("保留两位小数: " + df2.format(number)); }@Test public void formatDemo () { double number = 123456789.345D; // 每三位数加一个逗号 DecimalFormat df = new DecimalFormat("#,###.###"); System.out.println("千分位: " + df.format(number)); DecimalFormat df2 = new DecimalFormat("#,###.00"); System.out.println("保留两位小数: " + df2.format(number)); }
打印结果:
千分位: 123,456,789.345保留两位小数: 123,456,789.34千分位: 123,456,789.345 保留两位小数: 123,456,789.34千分位: 123,456,789.345 保留两位小数: 123,456,789.34
3. 百分数格式化
/*** 百分数** @param value* @return*/public static String toPercent(Double value) {DecimalFormat decimalFormat = new DecimalFormat("#0.00%");return decimalFormat.format(value);}/** * 百分数 * * @param value * @return */ public static String toPercent(Double value) { DecimalFormat decimalFormat = new DecimalFormat("#0.00%"); return decimalFormat.format(value); }/** * 百分数 * * @param value * @return */ public static String toPercent(Double value) { DecimalFormat decimalFormat = new DecimalFormat("#0.00%"); return decimalFormat.format(value); }
@Testpublic void calculate() {System.out.println("0.1234 -->" + toPercent(0.1234));System.out.println("0.00123 -->" + toPercent(0.00123));}@Test public void calculate() { System.out.println("0.1234 -->" + toPercent(0.1234)); System.out.println("0.00123 -->" + toPercent(0.00123)); }@Test public void calculate() { System.out.println("0.1234 -->" + toPercent(0.1234)); System.out.println("0.00123 -->" + toPercent(0.00123)); }
打印结果:
0.1234 -->12.34%0.00123 -->0.12%0.1234 -->12.34% 0.00123 -->0.12%0.1234 -->12.34% 0.00123 -->0.12%
【信息由网络或者个人提供,如有涉及版权请联系COOY资源网邮箱处理】
© 版权声明
部分内容为互联网分享,若有侵权请联系站长删除。
THE END
暂无评论内容