| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package cn.com.lzt.tools;
- import java.math.BigDecimal;
- import java.text.DecimalFormat;
- public class DecimalUtils {
-
- public static Double ZERO = formatToDouble(0);
-
- public static BigDecimal ZERODecimal = BigDecimal.ZERO;
- /**
- * 保留2位小数
- * 四舍五入
- * */
- public static String formatToStr(double x) {
- DecimalFormat df = new DecimalFormat("#.00");
- return df.format(x);
- }
- /**
- * 保留2位小数
- * 四舍五入
- * */
- public static double formatToDouble(double x) {
- x = (double) Math.round(x * 100) / 100;
- return x;
- }
- /**
- * 保留2位小数
- * 指定进位类型
- * */
- public static double formatToDouble(double d ,int roundingMode) {
- BigDecimal b = new BigDecimal(d);
- d = b.setScale(2, roundingMode).doubleValue();
- return d;
- }
-
- public static BigDecimal formatToDecimal(double x) {
- x = (double) Math.round(x * 100) / 100;
- BigDecimal d = new BigDecimal(String.valueOf(x));
- d.setScale(2,BigDecimal.ROUND_HALF_UP);
- return d;
- }
-
- }
|