java交易⾦额转换分_java对⾦额的元与分的转换java⾦额元与分转换:
1.元转分:
可传⼊字符串或者long型(隐式转换为long型也可)整数。
若传⼊为字符串,则通过替换⼩数点的⽅式转换,⼩数点有多位或者没有⼩数点皆已考虑。
若传⼊为long 则通过BigDecimal来乘100
2.分转元:
可传⼊字符串或者long型(隐式转换为long型也可)整数。并对传⼊的分进⾏粗略的格式判断
package com.westsoft.xpf.portal.utils;
import java.math.BigDecimal;
/**
* com.util.AmountUtils
java valueof
* @description ⾦额元分之间转换⼯具类
* @author zcm0708@sina
* @2012-2-7下午12:58:00
*/
public class AmountUtils {
/**⾦额为分的格式 */
public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";
/**
* 将分为单位的转换为元并返回⾦额格式的字符串 (除100)
*
* @param amount
* @return
* @throws Exception
*/
public static String changeF2Y(Long amount) throws Exception{
if(!String().matches(CURRENCY_FEN_REGEX)) {
throw new Exception("⾦额格式有误");
}
int flag = 0;
String amString = String();
if(amString.charAt(0)=='-'){
flag = 1;
amString = amString.substring(1);
}
StringBuffer result = new StringBuffer();
if(amString.length()==1){
result.append("0.0").append(amString);
}else if(amString.length() == 2){
result.append("0.").append(amString);
}else{
String intString = amString.substring(0,amString.length()-2);
for(int i=1; i<=intString.length();i++){
if( (i-1)%3 == 0 && i !=1){
result.append(",");
}
result.append(intString.substring(intString.length()-i,intString.length()-i+1)); }
if(flag == 1){
return "-"+String();
}else{
String();
}
}
/**
* 将分为单位的转换为元 (除100)
*
* @param amount
* @return
* @throws Exception
*/
public static String changeF2Y(String amount) throws Exception{
if(!amount.matches(CURRENCY_FEN_REGEX)) {
throw new Exception("⾦额格式有误");
}
return BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100)).toString(); }
/
**
* 将元为单位的转换为分 (乘100)
*
* @param amount
* @return
*/
public static String changeY2F(Long amount){
return BigDecimal.valueOf(amount).multiply(new BigDecimal(100)).toString();
}
/**
* 将元为单位的转换为分 替换⼩数点,⽀持以逗号区分的⾦额
*
* @param amount
* @return
*/
public static String changeY2F(String amount){
String currency = placeAll("\\$|\\¥|\\,", ""); //处理包含, ¥ 或者$的⾦额
int index = currency.indexOf(".");
int length = currency.length();
Long amLong = 0l;
if(index == -1){
amLong = Long.valueOf(currency+"00");
}else if(length - index >= 3){
amLong = Long.valueOf((currency.substring(0, index+3)).replace(".", ""));
}else if(length - index == 2){
amLong = Long.valueOf((currency.substring(0, index+2)).replace(".", "")+0);
}else{
amLong = Long.valueOf((currency.substring(0, index+1)).replace(".", "")+"00");
}
String();
}
public static void main(String[] args) {
// try {
/
/ System.out.println("结果:"+changeF2Y("-000a00"));
// } catch(Exception e){
// System.out.println("----------->>>"+e.getMessage());
ErrorCode();
// }
// System.out.println("结果:"+changeY2F("1.00000000001E10"));
System.out.println(AmountUtils.changeY2F("1.33"));
try {
System.out.println(AmountUtils.changeF2Y("1322"));
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println(Long.parseLong(AmountUtils.changeY2F("1000000000000000"))); // System.out.println(Integer.parseInt(AmountUtils.changeY2F("10000000")));
// System.out.println(Integer.MIN_VALUE);
// long a = 0;
// System.out.println(a);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。