📄 numberutil.java
字号:
package cn.st.util;
import java.text.DecimalFormat;
import java.text.NumberFormat;
/**
*
* @description 该类是一个数字格式化的类。目前提供简单的功能。
* @author zn
* @version
* @date 2006-8-18 下午12:27:49
*
*/
public class NumberUtil {
private static NumberFormat nf2 = null;
private static DecimalFormat df2 = null;
private static NumberFormat nf3 = null;
private static DecimalFormat df3 = null;
static {
// nf = NumberFormat.getNumberInstance(Locale.getDefault());
nf2 = NumberFormat.getNumberInstance();
nf2.setMaximumFractionDigits(2);
nf3 = NumberFormat.getNumberInstance();
nf3.setMaximumFractionDigits(3);
df2 = new DecimalFormat("0.00");
df3 = new DecimalFormat("0.000");
// df = (DecimalFormat) NumberFormat.getInstance();
// df.applyPattern("######,###.00");
// df = new DecimalFormat("######,000.00");
}
/**
* 格式化输入的Number。 保留小数位2位,四舍五入。如果是具有小数点。这格式化的结果为:
* xxx.xx.例子:
* double d = 100000000000.34999999;
* format(d)的结果是:100000000000.35
* @param value
* @return 格式化后的字符串
*/
public static String format2(Number value) {
return df2.format(value);
}
/**
* 格式化输入的Number。 保留小数点3位是具有小数点。四舍五入。这格式化的结果为:
* xxx.xx.例子:
* double d = 100000000000.34999999;
* format(d)的结果是:100000000000.35
* @param value
* @return 格式化后的字符串
*/
public static String format3(Number value) {
return df3.format(value);
}
/**
* 使用千分位分割数字。保留小数位2位,四舍五入。10000000000.12 = 10,000,000,000.12
* @author zn
* @date 2006-12-28 下午05:30:11
* @param value
* @return
*/
public static String format2Comma(Number value) {
String str = nf2.format(value);
if(!str.contains(".")) {
str += ".00";
}
return str;
}
/**
* 使用千分位分割数字,保留小数位3位,四舍五入
* 10000000000.12 = 10,000,000,000.12
* @description:
* @author:zn
* @datetime:2007-1-25-下午02:30:09
* @param value
* @return
*/
public static String format3Comma(Number value) {
String str = nf3.format(value);
if(!str.contains(".")) {
str += ".000";
}
return str;
}
/**
* 格式化输入的字符串。如果输入的字符串为非数字类型,则放回原值。否则返回被格式化的值
* 如果为正型,则输出为正型。否则保留小数点2位,四舍五入
* 例子:
* format(100.399) = 100.40
* format("abc") = abc;
* format("10") = 10;
* format("10.0") = 10.0
* @author zn
* @date 2006-12-28 下午02:38:30
* @param obj
* @return 返回被格式化后的数值。
* if string value is null, throws NullPointException.
*/
@SuppressWarnings("finally")
public static String format2ToString(String obj) {
String objTmp = obj;
try {
Integer integer = Integer.valueOf(objTmp);
df2 = new DecimalFormat("0");
objTmp = df2.format(integer);
} catch(NumberFormatException e) {
try {
df2 = new DecimalFormat("0.00");
Double value = Double.valueOf(obj);
objTmp = format2(value);
} catch(NumberFormatException e1) {
objTmp = obj;
}
} finally {
return objTmp;
}
}
/**
* 将0.00,0.0,0,0.000的字符串返回为" "
* @author:zn
* @datetime:2007-1-28-下午03:20:54
* @param value
* @return
*/
public static String formatZero(String value) {
value = (value.trim().equals("0.00") || value.trim().equals("0.0") || value
.trim().equals("0") || value.trim().equals("0.000")) ? " " : value;
return value;
}
/**
* 格式化输入的字符串。如果输入的字符串为非数字类型,则反回原值。否则返回被格式化的值
* 如果为正型,则输出为正型。否则保留小数点3位,四舍五入
* @description:
* @author:zn
* @datetime:2007-1-25-下午02:27:37
* @param obj
* @return
*/
@SuppressWarnings("finally")
public static String format3ToString(String obj) {
String objTmp = obj;
try {
Integer integer = Integer.valueOf(objTmp);
df3 = new DecimalFormat("0");
objTmp = df3.format(integer);
} catch(NumberFormatException e) {
try {
df3 = new DecimalFormat("0.000");
Double value = Double.valueOf(obj);
objTmp = format3(value);
} catch(NumberFormatException e1) {
objTmp = obj;
}
} finally {
return objTmp;
}
}
/**
* 解析一个Number对象,保留小数位2位。四舍五入。
* @author:zn
* @datetime:2007-1-25-下午03:19:52
* @param value
* @return
*/
public static Double parser2(Number value) {
return Double.parseDouble(format2(value));
}
/**
* 解析一个Number对象,保留小数位3位。四舍五入。
* @author:zn
* @datetime:2007-1-25-下午03:18:48
* @param value
* @return
*/
public static Double parser3(Number value) {
// Double dou = 0.0;
// try {
// dou = (Double) df3.parse(format3(value));
// } catch (ParseException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
return Double.valueOf(format3(value));
}
/**
* 根据数量和单价计算出总价。保留小数2位,四舍五入
* 实现内部将传入的量和价分别进行了3位的2位格式化。
* @author:zn
* @datetime:2007-1-25-下午03:28:04
* @param quantity 数量,任意小数位数
* @param unitPrice 单价 任意小数位数
* @return 总价
*/
public static Double calculateTotalPrice(Number quantity, Number unitPrice) {
Double totalPrice = (parser3(quantity)) * (parser2(unitPrice));
return parser2(totalPrice);
}
/**
* 根据总价和单价计算出数量。保留小数位数3。四舍五入
* 内部实现分别对量和价进行了3位和2位的格式化
* @author:zn
* @datetime:2007-1-25-下午03:35:44
* @param unitPrice 单价
* @param totalPrice 总价
* @return 数量
*/
public static Double calculateQuantity(Number unitPrice, Number totalPrice) {
//Double quantity = parser2(totalPrice) / parser2(unitPrice);
Double quantity = (Double)totalPrice / (Double)unitPrice;
return parser3(quantity);
}
public static void main(String [] args) {
double value = 60.0000;
// value = parser2(value);
// Double dou = Double.parseDouble(str);
System.out.println(parser2(60));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -