简书链接:BigDecimal的问题
文章字数:432,阅读全文大约需要1分钟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| 100.386->ROUND_HALF_EVEN 100.39 100.386->ROUND_HALF_UP 100.39 100.386->ROUND_HALF_DOWN 100.39 100.386->ROUND_UP 100.39 100.386->ROUND_DOWN 100.38 100.386->ROUND_FLOOR 100.38 100.386->ROUND_CEILING 100.39 ------------ 100.382->ROUND_HALF_EVEN 100.38 100.382->ROUND_HALF_UP 100.38 100.382->ROUND_HALF_DOWN 100.38 100.382->ROUND_UP 100.39 100.382->ROUND_DOWN 100.38 100.382->ROUND_FLOOR 100.38 100.382->ROUND_CEILING 100.39 ------------ 100.38->ROUND_HALF_EVEN 100.38 100.38->ROUND_HALF_UP 100.38 100.38->ROUND_HALF_DOWN 100.38 100.38->ROUND_UP 100.38 100.38->ROUND_DOWN 100.37 100.38->ROUND_FLOOR 100.37 100.38->ROUND_CEILING 100.38 ------------ 100.38->ROUND_HALF_EVEN 100.38 100.38->ROUND_HALF_UP 100.38 100.38->ROUND_HALF_DOWN 100.38 100.38->ROUND_UP 100.38 100.38->ROUND_DOWN 100.37 100.38->ROUND_FLOOR 100.37 100.38->ROUND_CEILING 100.38 ------------
BigDecimal bg = new BigDecimal(money); // double f1 = bg.setScale(2, BigDecimal.ROUND_FLOOR).doubleValue();//抹掉后面的。 // return f1;
String format = String.format("money %.2f", 100.356f);//100.36 DecimalFormat df = new DecimalFormat("#.00"); System.out.println("test:" + df.format(100.356f)); System.out.println(format);
NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); double f = 100.356f; System.out.println(nf.format(f));
|
// 经过测试系统的api,无论哪种只保留2位小数,第三位没有或者存在 都会导致出现不同的结果, 需求是不受后面有没有或者存在的影响,所以写了这个东西。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public static String keepFormatNoney(Object money, int keepCount) { String moneyStr = money + ""; int index = moneyStr.indexOf("."); if (index == -1) { return moneyStr; } else { int end; if ((index + keepCount + 1) > moneyStr.length()) { end = moneyStr.length();//index+2>s.length()?index+2:s.length() } else { end = index + keepCount + 1;
} String substring = moneyStr.substring(0, end); return substring; } }
System.out.println("final str " + keepFormatNoney(10.333,2)); System.out.println("final str " + keepFormatNoney(10.32345,2)); System.out.println("final str " + keepFormatNoney(103.3,3)); System.out.println("final str " + keepFormatNoney(103.3333,3));
final str 100.12 final str 10.33 final str 10.32 final str 103.3 final str 103.333
|
小数点 乘上100保留3位数的办法
1 2 3 4 5 6 7 8 9 10
| public static String formatMoney(float money) { int mymoney = (int) (money * 100); String format = String.format("%3d", mymoney); return format.replace(" ", "0"); /*if(money<10){ return "00"+money; }else{ return money+""; }*/ }
|
然而还是有问题
修正后
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public static String formatMoney(double money) { BigDecimal bigDecimal = new BigDecimal(Double.toString(money)); int mymoney = bigDecimal.multiply(new BigDecimal(Double.toString(100))).intValue(); /* bigDecimal = new BigDecimal(Double.toString(mymoney)); bigDecimal.setScale(2, BigDecimal.ROUND_DOWN); double reverseValue = bigDecimal.divide(new BigDecimal(Double.toString(100))).doubleValue();
if (reverseValue != money) { throw new RuntimeException("rever error:" + moneyStr + ",doResult:" + reverseValue); }*/ String format = String.format("%3d", mymoney); return format.replace(" ", "0"); } public static String formatMoney(String moneyStr) { double money = Double.parseDouble(moneyStr); return formatMoney(money); }
|