数字类型、字符串类型常用转换,数据精度问题解决

价格,重量,数量保留n位小数,不足补0。

常用数据类型转化的问题。


/**

* 重量数字保留三位有效数字,不足补0
* @param weight
* @return
*/
public static String weightFormat(String weight){
if("".equals(weight))
return "";
try {
return formatWeight(Double.parseDouble(weight));
} catch (NumberFormatException e) {
return weight; //55--->55.000
}
}


/**
* 重量格式化 默认保留3位小数

* @param weight
*            重量
* @return
*/
public static String formatWeight(double weight) {
return formatDouble(weight, 3);// 3位小数
}



/**
* 重量四舍五入,避免精度丢失问题

* @param d
* @param scale
*            保留位数
* @return
*/
public static double roundDouble(double d, int scale) {
return new BigDecimal(d).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}


/**
* 重量四舍五入,避免精度丢失问题

* @param weight
* @return
*/
public static double roundWeight(double weight) {
return roundDouble(weight, 3);
}

/**
* 重量比較大小

* @param weight_a
* @param weight_b
* @return a>b 1;a=b 0;a<b -1;
*/
public static int compareWeight(double weight_a, double weight_b) {
return Double.compare(roundWeight(weight_a), roundWeight(weight_b));
}



/**
* 价格数字保留2位有效数字,不足补0
* @param price
* @return
*/
public String priceFormat(String price){
if("".equals(price) || price == null)
return "";
try {
return formatYuan(Double.parseDouble(price));
} catch (NumberFormatException e) {
return price;
}
}

/**
* 价格小数位处理
* @param price
* @param flag
* @return
*/
public String priceFormat(String price,boolean flag){
if("".equals(price) || price == null)
return "";
if(flag){
try {
return formatFen2yuan(Long.parseLong(price));
} catch (NumberFormatException e) {
return price;
}
}else{
return  priceFormat(price);
}
}

/**
* 货币转换接口:分-->元 ,格式化 数字字符串#.00

* @param fen 分
* @return 返回转换后的: String
*/
public static String formatFen2yuan(long fen)
{
DecimalFormat db = new DecimalFormat("#0.00");
return db.format(fen2yuan(fen));
}



/**
* 货币转换接口:元-->分

* @param yuan 元
* @return 返回转换后的: 分
*/
public static long yuan2fen(double yuan)
{
return (long) (Math.round(yuan * 100));
}







/**
* 货币转换接口:元-->分 ,格式化 数字字符串

* @param yuan 元
* @return 返回转换后的: String
*/
public static String formatYuan2fen(double yuan)
{
DecimalFormat db = new DecimalFormat("#");


return db.format(yuan2fen(yuan));
}

/**
* 货币转换接口:分-->元

* @param fen 分
* @return 返回转换后的: 元
*/
public static double fen2yuan(long fen)
{
return fen / 100.00;
}


/**
* 金额格式化 默认保留2位小数

* @param price
*            元
* @return
*/
public static String formatYuan(double price) {
return formatDouble(price, 2);// 2位小数
}


/**
* double格式化 主要用于前台显示

* @param d
* @param scale保留位数
* @return
*/
public static String formatDouble(double d, int scale) {
StringBuffer pattern = new StringBuffer("0");
int scaleTemp = scale;
if (scale > 0)
pattern.append(".");
while (scaleTemp-- > 0) {
pattern.append("0");
}
DecimalFormat df = new DecimalFormat(pattern.toString());
return df.format(roundDouble(d, scale));
}


/**
* 字符串太长,截取一定长度后加.....
* @param str
* @param length
* @return
*/
public String stringFormat(String str , int length){
if(str.length()<=length){
return str;
}else{
str = str.substring(0, length);
str = str + "...";
return str;
}
}

/**
* 小数的四舍五入

* @param in 浮点型小数
* @return 四舍五入后的整型数据
*/
public static int float2int(float in)
{
int flag = 1;
if (in < 0)
flag = -1;
int out = (int) (Math.abs(in) + 0.5000001);
return out * flag;
}








/**
* 小数的四舍五入

* @param in 双精度小数
* @return 长整型
*/
public static long double2long(double in)
{
long flag = 1;
if (in < 0)
flag = -1;
long out = (long) (Math.abs(in) + 0.5000001);
return out * flag;
}






/**
* 字符串转换为int型

* @param str 数字字符串
* @param defaultValue 缺省值
* @return 转换后的整型
*/
public static int str2int(String str, int defaultValue)
{
try
{
return new Integer(str.trim()).intValue();
}
catch (Exception e)
{
return defaultValue;
}
}





/**
* 字符串转换为int型<br>
* 缺省值为0

* @param str 数字字符串
* @return 转换后的整型数据
*/
public static int str2int(String str)
{
return str2int(str, 0);
}





/**
* 将其他类型的数字转换为int<br>
* 如果obj不是Number的子类,则必须实现toString方法来返回数字的字符串<br>
* 否则返回0


* @param obj
* @return 转换后的整型数据
*/
public static int gint(Object obj)
{
int res = 0;
if (obj == null)
{
}
else if (obj instanceof Number)
{
res = ((Number) obj).intValue();
}
else
{
res = str2int(obj.toString());
}
return res;
}





/**
* 字符串转换为长整型

* @param str 数字字符串"1234567890"
* @param defaultValue 缺省值
* @return 转换后的长整型
*/
public static long str2long(String str, long defaultValue)
{
try
{
return new Long(str.trim()).longValue();
}
catch (Exception e)
{
return defaultValue;
}
}





/**
* 字符串转换为长整型<br>
* 缺省值为0

* @param str 数字字符串"1234567890"
* @return 转换后的长整型
*/
public static long str2long(String str)
{
return str2long(str, 0);
}





/**
* Object对象转Long

* @param obj
* @return
*/
public static long glong(Object obj)
{
long res = 0;
if (obj == null)
{
}else if (obj instanceof Double){

return double2long(Double.valueOf(""+obj));
}
else if (obj instanceof Number)
{
res = ((Number) obj).longValue();
}
else
{
System.out.println("string="+obj.toString());
res = str2long(obj.toString());
}
return res;
}





/**
* 字符串转换为浮点型

* @param str 数字型的字符串"123456.123456"
* @param defaultValue 缺省值
* @return 转换后的浮点型值
*/
public static float str2float(String str, float defaultValue)
{
try
{
return new Float(str.trim()).floatValue();
}
catch (Exception e)
{
return defaultValue;
}
}





/**
* 字符串转换为浮点型<br>
* 缺省值为0

* @param str 数字型的字符串"123456.123456"
* @return 转换后的浮点型值
*/
public static float str2float(String str)
{
return str2float(str, 0);
}




/**
* Object对象转float

* @param obj
* @return
*/
public static float gfloat(Object obj)
{
float res = 0;
if (obj == null)
{
}
else if (obj instanceof Number)
{
res = ((Number) obj).floatValue();
}
else
{
res = str2float(obj.toString());
}
return res;
}



/**
* 字符串转换为双精度型

* @param str 数字型的字符串"123456.123456"
* @param defaultValue 缺省值
* @return 转换后的双精度型
*/
public static double str2double(String str, double defaultValue)
{
try
{
str = str.trim();
char[] s = str.toCharArray();
StringBuffer ret = new StringBuffer();
int ct = 0;
boolean star = false;
for(int i= 0;i < s.length;i++){

if(star){ct ++;}

if(ct > 6){break;}

if(s[i] == '.'){
star = true;
}
ret.append(s[i]);
}
str = ret.toString();
return new Double(str).doubleValue();
}
catch (Exception e)
{
return defaultValue;
}
}





/**
* 数字型字符串转换为双精度型<br>
* 缺省值为0

* @param str 数字型的字符串"123456.123456"
* @return 转换后的双精度型
*/
public static double str2double(String str)
{
return str2double(str, 0);
}





/**
* Object对象转Double

* @param obj
* @return
*/
public static double gdouble(Object obj)
{
double res = 0;
if (obj == null)
{
}
else if (obj instanceof Number)
{
res = ((Number) obj).doubleValue();
}
else
{
res = str2double(obj.toString());
}
return res;
}





/**
* Object对象转String

* @param obj
* @return
*/
public static String gstring(Object obj)
{
String s = "";
if (obj != null)
{
if (obj instanceof String)
{
s = (String) obj;
}else
{
s = obj.toString();
}
}
return s;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值