?? numberutil.java
字號:
package cn.st.util;
import java.text.DecimalFormat;
import java.text.NumberFormat;
/**
*
* @description 該類是一個數(shù)字格式化的類。目前提供簡單的功能。
* @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。 保留小數(shù)位2位,四舍五入。如果是具有小數(shù)點(diǎn)。這格式化的結(jié)果為:
* xxx.xx.例子:
* double d = 100000000000.34999999;
* format(d)的結(jié)果是:100000000000.35
* @param value
* @return 格式化后的字符串
*/
public static String format2(Number value) {
return df2.format(value);
}
/**
* 格式化輸入的Number。 保留小數(shù)點(diǎn)3位是具有小數(shù)點(diǎn)。四舍五入。這格式化的結(jié)果為:
* xxx.xx.例子:
* double d = 100000000000.34999999;
* format(d)的結(jié)果是:100000000000.35
* @param value
* @return 格式化后的字符串
*/
public static String format3(Number value) {
return df3.format(value);
}
/**
* 使用千分位分割數(shù)字。保留小數(shù)位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;
}
/**
* 使用千分位分割數(shù)字,保留小數(shù)位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;
}
/**
* 格式化輸入的字符串。如果輸入的字符串為非數(shù)字類型,則放回原值。否則返回被格式化的值
* 如果為正型,則輸出為正型。否則保留小數(shù)點(diǎn)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 返回被格式化后的數(shù)值。
* 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;
}
/**
* 格式化輸入的字符串。如果輸入的字符串為非數(shù)字類型,則反回原值。否則返回被格式化的值
* 如果為正型,則輸出為正型。否則保留小數(shù)點(diǎn)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對象,保留小數(shù)位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對象,保留小數(shù)位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));
}
/**
* 根據(jù)數(shù)量和單價計算出總價。保留小數(shù)2位,四舍五入
* 實(shí)現(xiàn)內(nèi)部將傳入的量和價分別進(jìn)行了3位的2位格式化。
* @author:zn
* @datetime:2007-1-25-下午03:28:04
* @param quantity 數(shù)量,任意小數(shù)位數(shù)
* @param unitPrice 單價 任意小數(shù)位數(shù)
* @return 總價
*/
public static Double calculateTotalPrice(Number quantity, Number unitPrice) {
Double totalPrice = (parser3(quantity)) * (parser2(unitPrice));
return parser2(totalPrice);
}
/**
* 根據(jù)總價和單價計算出數(shù)量。保留小數(shù)位數(shù)3。四舍五入
* 內(nèi)部實(shí)現(xiàn)分別對量和價進(jìn)行了3位和2位的格式化
* @author:zn
* @datetime:2007-1-25-下午03:35:44
* @param unitPrice 單價
* @param totalPrice 總價
* @return 數(shù)量
*/
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));
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -