//这里面主要是由于银联的手机支付用到了一个付款的转换方式问题,当调用支付宝付款的时候,比如一分钱格式就是我们普通用的格式:0.01,而银联确要把格式转化为000000000001(银联要求是12位)这种格式的,最后一位是分,然后向前依次是角,元,十元,百元,相信不用我说,你已经懂了吧。所以这里就需要一个转换,为了应对像1000,1000.00,1000.0这种传过来的格式的数据,特做一下转换,然后传给银联进行支付。
package com.abin.lee.collection.inner;
public class ConsumeFinance {
public static enum Currency{
CHINARMB("chinarmb"),DOLLAR("dollar"),HK("hk");
private Currency(String textVal){
this.textVal=textVal;
}
private String textVal;
public String toString(){
return textVal;
}
}
public static String ConsumeProcess(String money,Currency currency){
System.out.println("currentcy="+currency);
String flag=null;
if(currency.equals(Currency.CHINARMB)){
ConsumeFinance.ChinaProcess chinaProcess=new ConsumeFinance.ChinaProcess();
flag=chinaProcess.chinaChange(money, currency);
}
return flag;
}
public static String ConsumeProcessReturn(String money,Currency currency){
System.out.println("currentcy="+currency);
String flag=null;
if(currency.equals(Currency.CHINARMB)){
ConsumeFinance.ChinaReturn chinaReturn=new ConsumeFinance.ChinaReturn();
flag=chinaReturn.chinaReChange(money, currency);
}
return flag;
}
public static class ChinaProcess{
private static String status=null;
public static String chinaChange(String money,Currency currency){
int point=money.lastIndexOf(".");
if(point==-1){
StringBuffer stb=new StringBuffer();
int len=money.length();
int lon=10-len;
for(int i=0;i<lon;i++){
stb.append("0");
}
stb.append(money);
stb.append("00");
status=stb.toString().intern();
}
if(point>0){
int len=money.indexOf(".");
int chn=money.length()-len-1;
switch(chn){
//先判断money格式为100.0这种格式的,小数点后面有一位小数点的情况
case 1:
StringBuffer stb=new StringBuffer(money);
stb.deleteCharAt(len);
int lend=stb.length();
StringBuffer sb=new StringBuffer();
for(int i=0;i<12-lend;i++){
sb.append("0");
}
sb.append(stb.toString());
status=sb.toString().intern();
//先判断money格式为100.01这种格式的,小数点后面有两位小数点的情况
case 2:
money=money.replace(".", "");
int lende=money.length();
String connect="";
for(int i=0;i<12-lende;i++){
connect+="0";
}
status=connect+money;
}
}
return status;
}
}
//把000000001001转化为10.01这种格式
public static class ChinaReturn{
private static String status=null;
public static String chinaReChange(String money,Currency currency){
int change=money.length();
int num=0;
for(int i=0;i<change;i++){
int value=Integer.valueOf(String.valueOf(money.charAt(i)));
if(value>0){
num=i;
break;
}
}
money=money.substring(num);
if(num==10){
status="0."+money;
}
if(num==11){
status="0.0"+money;
}
if(num<10){
System.out.println(money.length());
StringBuffer stb=new StringBuffer(money);
stb.insert(money.length()-2, ".");
status=stb.toString().intern();
}
return status;
}
}
public static void main(String[] args) {
Currency currency=Currency.CHINARMB;
String money="000000001001";
String result=ConsumeProcessReturn(money, currency);
// String result=ConsumeProcess(money, currency);
System.out.println("result="+result);
}
}