📄 numberformatutil.java
字号:
}catch(NumberFormatException e) {
NumberFormatOfException ne = new NumberFormatOfException(e.getMessage());
ne.setExtendMessage(s);
ne.setMessageCode("resource_NumberFormatUtil_001");
ne.setMessageFileName(Constants.MESSAGE_FILENAME);
throw ne;
}
}
/**
* 把字符串转换成为单精度数字
*
* @param s 要转换的字符串
* @return 单精度数字
*/
public static float toFloat(String s) {
return toCFloat(s).floatValue();
}
/**
* 把字符串转换成为单精度数字类
*
* @param s 要转换的字符串
* @return 单精度数字类
* @throws NumberFormatOfException 字符格式不正确时报异常
*/
public static Float toCFloat(String s) {
if (s == null)
return null;
try{
String str = toToken(s);
return Float.valueOf(str);
}catch(NumberFormatException e) {
NumberFormatOfException ne = new NumberFormatOfException(e.getMessage());
ne.setExtendMessage(s);
ne.setMessageFileName(Constants.MESSAGE_FILENAME);
ne.setMessageCode("resource_NumberFormatUtil_001");
throw ne;
}
}
/**
* 把字符串转换成为长整形数字
*
* @param s 要转换的字符串
* @return 长整形数字
*/
public static long toLong(String s) {
return toClong(s).longValue();
}
/**
* 把字符串转换成为长整形数字类
*
* @param s 要转换的字符串
* @return 长整形数字类
* @throws NumberFormatOfException 字符格式不正确时报异常
*/
public static Long toClong(String s) {
if (s == null)
return null;
try{
String str = toToken(s);
return Long.valueOf(str);
}catch(NumberFormatException e) {
NumberFormatOfException ne = new NumberFormatOfException(e.getMessage());
ne.setExtendMessage(s);
ne.setMessageFileName(Constants.MESSAGE_FILENAME);
ne.setMessageCode("resource_NumberFormatUtil_001");
throw ne;
}
}
/**
* 把字符串转换成为整形数字
*
* @param s 要转换的字符串
* @return 整形数字
*/
public static int toInt(String s) {
return toCInt(s).intValue();
}
/**
* 把字符串转换成为整形数字类
*
* @param s 要转换的字符串
* @return 整形数字类
* @throws NumberFormatOfException 字符格式不正确时报异常
*/
public static Integer toCInt(String s) {
if (s == null)
return new Integer(0);
try{
String str = toToken(s);
return Integer.valueOf(str);
}catch(NumberFormatException e) {
NumberFormatOfException ne = new NumberFormatOfException(e.getMessage());
ne.setExtendMessage(s);
ne.setMessageFileName(Constants.MESSAGE_FILENAME);
ne.setMessageCode("resource_NumberFormatUtil_001");
throw ne;
}
}
/**
* 格式化双精度数字为“###0.00”标准货币格式
*
* @param d 需要格式化的双精度数字
* @return 返回格式化过的字符串
*/
public static String formatCurr(double d) {
String strNumber = null;
DecimalFormat df = new DecimalFormat("###0.00");
strNumber = df.format(d);
return strNumber;
}
/**
* 格式化双精度数字类为“###0.00”标准格式
*
* @param d 需要格式化的双精度数字类
* @return 返回格式化过的字符串
*/
public static String formatCurr(Double d) {
if (d == null) {
return null;
} else {
double dd = d.doubleValue();
return NumberFormatUtil.formatCurr(dd);
}
}
/**
* 格式化双精度数字为“###0.0###”标准格式
*
* @param d 需要格式化的双精度数字
* @return 返回格式化过的字符串
*/
public static String formatData(double d) {
String strNumber = null;
DecimalFormat df = new DecimalFormat("###0.0###");
strNumber = df.format(d);
return strNumber;
}
/**
* 格式化双精度数字为“###0.00”标准货币格式
*
* @param d 需要格式化的双精度数字
* @return 返回格式化过的字符串
* @see #format(double)
* @see #formatCurr(double)
* @see #formatCurr(Double)
* @see #formatData(double)
* @see #formatPrice(Double)
*/
public static String formatPrice(double d) {
String strNumber = null;
DecimalFormat df = new DecimalFormat("###0.00");
strNumber = df.format(d);
return strNumber;
}
/**
* 格式化双精度数字类为“###0.00”标准货币格式
*
* @param d 需要格式化的双精度数字类
* @return 返回格式化过的字符串
* @see #format(double)
* @see #formatCurr(double)
* @see #formatCurr(Double)
* @see #formatData(double)
* @see #formatPrice(double)
*/
public static String formatPrice(Double d) {
if (d == null) {
return null;
} else {
double dd = d.doubleValue();
return NumberFormatUtil.formatPrice(dd);
}
}
/**
* 格式化双精度数字为“###0.###”标准数字格式
*
* @param d 需要格式化的双精度数字
* @return 返回格式化过的字符串
*/
public static String formatNum(double d) {
String strNumber = null;
DecimalFormat df = new DecimalFormat("###0.###");
strNumber = df.format(d);
return strNumber;
}
/**
* 格式化双精度数字类为“###0.###”标准数字格式
* @param d 需要格式化的双精度数字类
* @return 返回格式化过的字符串
*/
public static String formatNum(Double d) {
if (d == null) {
return null;
} else {
double dd = d.doubleValue();
return NumberFormatUtil.formatNum(dd);
}
}
/**
* 格式化双精度数字为数据库可以接受的标准数字格式
*
* @param d 需要格式化的双精度数字
* @param i 如果为正数,则为小数位数;如果为负数,则舍弃小数部分并向右舍弃相应位数值为0
* @return 返回格式化过的字符串
*/
public static double formatToDb(double d, int i) {
String dd=null;
try{
dd = formatNoComma(d,i);
return Double.parseDouble(dd);
}catch(NumberFormatException e) {
NumberFormatOfException ne = new NumberFormatOfException(e.getMessage());
ne.setExtendMessage(dd);
ne.setMessageFileName(Constants.MESSAGE_FILENAME);
ne.setMessageCode("resource_NumberFormatUtil_001");
throw ne;
}
}
/**
* 格式化双精度数字为###########0.###################数字格式
*
* @param d 需要格式化的双精度数字
* @return 返回格式化过的字符串
*/
public static String formatRate(double d) {
String strNumber = null;
DecimalFormat df = new DecimalFormat("###########0.###################");
strNumber = df.format(d);
return strNumber;
}
/**
* 根据选择不同的人数范围标签,得到人数范围
* for 商务圈统计报表
* @param flag 人数范围标志(1-50人、50-100人、100-150人、150-200人、200人以上)
*/
public static Long[] getMemberNumberRange(String flag){
int flagInt=0;
try{
flagInt=Integer.parseInt(flag);
}
catch(Exception e){
System.out.println(e.getMessage());
return null;
}
Long[] result = new Long[2];
try{
switch(flagInt){
case 1:
result[0]=new Long(1);
result[1]=new Long(50);
break;
case 2:
result[0]=new Long(51);
result[1]=new Long(100);
break;
case 3:
result[0]=new Long(101);
result[1]=new Long(150);
break;
case 4:
result[0]=new Long(151);
result[1]=new Long(200);
break;
case 5:
result[0]=new Long(201);
result[1]=new Long(Long.MAX_VALUE);
break;
default:
result[0]=new Long(1);
result[1]=new Long(Long.MAX_VALUE);
break;
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -