刷卡记录排序和灌溉记录小数点修改
This commit is contained in:
@ -0,0 +1,84 @@
|
||||
package com.fastbee.common.utils;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* 数字工具类
|
||||
*/
|
||||
public class NumberUtils {
|
||||
|
||||
/**
|
||||
* 保留两位小数,不足补0
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
public static float formatFloat(float number) {
|
||||
return (float) (Math.round(number * 100) / 100.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保留两位小数,不足补0
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
public static Double formatDouble(Double number) {
|
||||
return (Double) (Math.round(number * 100) / 100.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串类型保留两位小数,不足补0
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String formatString(String str) {
|
||||
if (isNumeric(str)) {
|
||||
float number = Float.parseFloat(str);
|
||||
DecimalFormat df = new DecimalFormat("0.00");
|
||||
return df.format(number);
|
||||
} else {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串类型保留三位小数,不足补0
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String formatThreeString(String str) {
|
||||
if (isNumeric(str)) {
|
||||
float number = Float.parseFloat(str);
|
||||
DecimalFormat df = new DecimalFormat("0.000");
|
||||
return df.format(number);
|
||||
} else {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串类型保留三位小数,不足补0
|
||||
* @param str
|
||||
* @param format 样式例如0.000
|
||||
* @return
|
||||
*/
|
||||
public static String formatStringByFormat(String str,String format) {
|
||||
if (isNumeric(str)) {
|
||||
float number = Float.parseFloat(str);
|
||||
DecimalFormat df = new DecimalFormat(format);
|
||||
return df.format(number);
|
||||
} else {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 正则判断是否为数字
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static boolean isNumeric(String str) {
|
||||
return str.matches("-?\\d+(\\.\\d+)?");
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user