分转元并 BigDecimal去掉小数点后00无用值
作者:互联网
分转元并去掉小数点后面无用值 public static String convertFen2Yuan(String fen) { BigDecimal fenAmt = new BigDecimal(fen); BigDecimal yuanAmt = fenAmt.divide(new BigDecimal(100)).setScale(2, RoundingMode.DOWN).stripTrailingZeros(); return yuanAmt.toString(); }
测试:
如果不论小数点后面数字为多少,都保留两位的话则吧.stripTrailingZeros()方法去掉
public static String convertFen2Yuan(String fen) { BigDecimal fenAmt = new BigDecimal(fen); BigDecimal yuanAmt = fenAmt.divide(new BigDecimal(100)).setScale(2, RoundingMode.DOWN); return yuanAmt.toString(); }
测试:
如果转字符串的话,加.toPlainString()就行,比如
new BigDecimal("95.000").stripTrailingZeros().toPlainString()
标签:00,BigDecimal,fen,fenAmt,yuanAmt,new,分转元,String 来源: https://blog.csdn.net/qq_27753441/article/details/112302899