📄 numberformatutil.java
字号:
package com.common.util;
import java.text.DecimalFormat;
import java.util.StringTokenizer;
import com.emk.manage.Constants;
import com.emk.manage.exception.NumberFormatOfException;
/**
* 格式化数字的功能
* @author WuQiaoYun
*/
public final class NumberFormatUtil {
/**
* 格式化双精度数字为“#,##0.00”标准货币格式
*
* @param d 需要格式化的双精度数字
* @return 返回格式化过的字符串
*/
public static String format(double d) {
DecimalFormat df = new DecimalFormat("#,##0.00");
return df.format(d);
}
/**
* 格式化单精度数字为“#,##0.00”标准货币格式
*
* @param f 需要格式化的单精度数字
* @return 返回格式化过的字符串
*/
public static String format(float f) {
return NumberFormatUtil.format((double)f);
}
/**
* 格式化长整形数字为“#,##0”格式
*
* @param l 需要格式化的长整形数字
* @return 返回格式化过的字符串
*/
public static String format(long l) {
DecimalFormat df = new DecimalFormat("#,##0");
return df.format(l);
}
/**
* 格式化整形数字为“#,##0”格式
*
* @param i 需要格式化的整形数字
* @return 返回格式化过的字符串
*/
public static String format(int i) {
return format((long) i);
}
/**
* 格式化整形数字为标准格式
*
* @param i 需要格式化的整形数字
* @param style 具体整形数字格式,如果style为null或为空字符串,则默认为“0000000000”
* @return 返回格式化过的字符串
*/
public static String format(int i, String style) {
String styleSelf;
if (style != null && !style.equals(""))
styleSelf = style;
else
styleSelf = "0000000000";
DecimalFormat df = new DecimalFormat(styleSelf);
return df.format(i);
}
/**
* 格式化双精度数字类
*
* @param d 双精度数字类
* @return 返回格式化过的字符串
*/
public static String format(Double d) {
if (d == null) {
return null;
} else {
double dd = d.doubleValue();
return NumberFormatUtil.format(dd);
}
}
/**
* 格式化单精度数字类
*
* @param f 单精度数字类
* @return 返回格式化过的字符串
*/
public static String format(Float f) {
if (f == null) {
return null;
} else {
float ff = f.floatValue();
return NumberFormatUtil.format(ff);
}
}
/**
* 格式化长整形数字类
*
* @param l 长整形数字类
* @return 返回格式化过的字符串
*/
public static String format(Long l) {
if (l == null) {
return null;
} else {
long ll = l.longValue();
return NumberFormatUtil.format(ll);
}
}
/**
* 格式化整形数字类
*
* @param i 整形数字类
* @return 返回格式化过的字符串
*/
public static String format(Integer i) {
if (i == null) {
return null;
} else {
int ii = i.intValue();
return NumberFormatUtil.format(ii);
}
}
/**
* 格式化双精度数字,保留一定的小数位数
*
* @param d 双精度数字
* @param decimalDigits 如果为正数,则为小数位数;如果为负数,则舍弃小数部分并向右舍弃相应位数值为0
* @return 返回格式化过的字符串
*/
public static String format(double d, int decimalDigits) {
String strNumber, symbol;
if (decimalDigits >= 0) {
symbol = "#,##0.";
for (int i = 0; i < decimalDigits; i++) {
symbol = symbol + "0";
}
DecimalFormat df = new DecimalFormat(symbol);
strNumber = df.format(d);
} else {
String strNum = NumberFormatUtil.format(d);
int inDot = strNum.indexOf('.');
//if (inDot + decimalDigits > 1) {
if (inDot + decimalDigits > 0) {
strNumber = strNum.substring(0, inDot + decimalDigits-1);
for (int i = decimalDigits; i < 0; i++) {
strNumber = strNumber + "0";
}
} else {
//strNumber = null;
strNumber = "0";
}
}
return strNumber;
}
/**
* 格式化双精度数字,保留一定的小数位数
*
* @param d 双精度数字
* @param decimalDigits 如果为正数,则为小数位数;如果为负数,则舍弃小数部分并向右舍弃相应位数值为0
* @return 格式化之后的字符串
*/
public static String formatNoComma(double d, int decimalDigits) {
String strNumber, symbol;
if (decimalDigits >= 0) {
symbol = "###0.";
for (int i = 0; i < decimalDigits; i++) {
symbol = symbol + "0";
}
DecimalFormat df = new DecimalFormat(symbol);
strNumber = df.format(d);
} else {
String strNum = NumberFormatUtil.format(d);
int inDot = strNum.indexOf('.');
if (inDot + decimalDigits > 1) {
strNumber = strNum.substring(0, inDot + decimalDigits-1);
for (int i = decimalDigits; i < 0; i++) {
strNumber = strNumber + "0";
}
} else {
strNumber = null;
}
}
return strNumber;
}
/**
* 格式化单精度数字,保留一定的小数位数
*
* @param f 单精度数字
* @param decimalDigits 如果为正数,则为小数位数;如果为负数,则舍弃小数部分并向右舍弃相应位数值为0
* @return 返回格式化过的字符串
*/
public static String format(float f, int decimalDigits) {
return NumberFormatUtil.format((double) f, decimalDigits);
}
/**
* 格式化双精度数字类,保留一定的小数位数
* 比如:NumberFormatUtil.format(1234567890.123456789012345678901d, -5)=1234500000
*
* @param d 双精度数字类
* @param decimalDigits 如果为正数,则为小数位数;如果为负数,则舍弃小数部分并向右舍弃相应位数值为0
* @return 返回格式化过的字符串
*/
public static String format(Double d, int decimalDigits) {
if (d == null) {
return null;
} else {
double dd = d.doubleValue();
return NumberFormatUtil.format(dd, decimalDigits);
}
}
/**
* 格式化双精度数字类,保留一定的小数位数
*
* @param d 双精度数字类
* @param decimalDigits 如果为正数,则为小数位数;如果为负数,则舍弃小数部分并向右舍弃相应位数值为0
* @return 返回格式化过的字符串
*/
public static String formatNoComma(Double d, int decimalDigits) {
if (d == null) {
return null;
} else {
double dd = d.doubleValue();
return NumberFormatUtil.formatNoComma(dd, decimalDigits);
}
}
/**
* 格式化单精度数字类,保留一定的小数位数
*
* @param f 单精度数字类
* @param decimalDigits 如果为正数,则为小数位数;如果为负数,则舍弃小数部分并向右舍弃相应位数值为0
* @return 返回格式化过的字符串
*/
public static String format(Float f, int decimalDigits) {
if (f == null) {
return null;
} else {
float ff = f.floatValue();
return NumberFormatUtil.format(ff, decimalDigits);
}
}
/**
* 除去字符串中的所有逗号分割符(,)
*
* @param s 需要处理的字符串
* @return 不含有逗号的字符串
*/
private static String toToken(String s) {
String newStr = new String("");
StringTokenizer st = new StringTokenizer(s, ",");
while (st.hasMoreTokens()) {
newStr = newStr + st.nextToken();
}
return newStr;
}
/**
* 把字符串转换成为双精度数字
*
* @param s 要转换的字符串
* @return 双精度数字
*/
public static double toDouble(String s) {
return toCDouble(s).doubleValue();
}
/**
* 把字符串转换成为双精度数字类
*
* @param s 要转换的字符串
* @return 双精度数字类
* @throws NumberFormatOfException 字符格式不正确时报异常
*/
public static Double toCDouble(String s) {
if (s == null) return null;
try{
String str = toToken(s);
return Double.valueOf(str);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -