金融行业字符,数值类型转换


package util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.List;

/**
* 金融类转换
* @author Administrator
*
*/
public class TradePortalUtil {
/***
* 将list转为数组
*
* @param list
* @return
*/
public static String[] ListToArray(List<String> list) {
String[] array = new String[list.size()];
list.toArray(array);
return array;
}

/**
* 处理金额,保留两位小数,并且整数位每三位用逗号分开
*
* @param money
* @return
*/
public static String splitMoney(double money) {
DecimalFormat df = new DecimalFormat("###,###,###.##");
df.applyPattern("#,##0.00#");
double rtnVal = Double.parseDouble(String.valueOf(money));
return df.format(rtnVal);
}

/**
* 还原金额
*
* @param money
* @return
* @throws ParseException
*/
public static double restoreMoney(String money) throws ParseException {
DecimalFormat df = new DecimalFormat("###,###,###.##");
return df.parse(money).doubleValue();
}

/**
* 金额元转分
* @see 注意:该方法可处理贰仟万以内的金额,且若有小数位,则不限小数位的长度
* @see 注意:如果你的金额达到了贰仟万以上,则不推荐使用该方法,否则计算出来的结果会令人大吃一惊
* @param amount 金额的元进制字符串
* @return String 金额的分进制字符串
*/
public static String moneyYuanToFen(String amount){
if(isEmpty(amount)){
return amount;
}
//传入的金额字符串代表的是一个整数
if(-1 == amount.indexOf(".")){
return Integer.parseInt(amount) * 100 + "";
}
//传入的金额字符串里面含小数点-->取小数点前面的字符串,并将之转换成单位为分的整数表示
int money_fen = Integer.parseInt(amount.substring(0, amount.indexOf("."))) * 100;
//取到小数点后面的字符串
String pointBehind = (amount.substring(amount.indexOf(".") + 1));
//amount=12.3
if(pointBehind.length() == 1){
return money_fen + Integer.parseInt(pointBehind)*10 + "";
}
//小数点后面的第一位字符串的整数表示
int pointString_1 = Integer.parseInt(pointBehind.substring(0, 1));
//小数点后面的第二位字符串的整数表示
int pointString_2 = Integer.parseInt(pointBehind.substring(1, 2));
//amount==12.03,amount=12.00,amount=12.30
if(pointString_1 == 0){
return money_fen + pointString_2 + "";
}else{
return money_fen + pointString_1*10 + pointString_2 + "";
}
}
/**
* 判断输入的字符串参数是否为空
* @return boolean 空则返回true,非空则flase
*/
public static boolean isEmpty(String input) {
return null==input || 0==input.length() || 0==input.replaceAll("\\s", "").length();
}

/**
* 金额分转元
* @see 注意:如果传入的参数中含小数点,则直接原样返回
* @see 该方法返回的金额字符串格式为<code>00.00</code>,其整数位有且至少有一个,小数位有且长度固定为2
* @param amount 金额的分进制字符串
* @return String 金额的元进制字符串
*/
public static String moneyFenToYuan(String amount){
if(isEmpty(amount)){
return amount;
}
if(amount.indexOf(".") > -1){
return amount;
}
if(amount.length() == 1){
return "0.0" + amount;
}else if(amount.length() == 2){
return "0." + amount;
}else{
return amount.substring(0, amount.length()-2) + "." + amount.substring(amount.length()-2);
}
}




}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值