将人民币中的阿拉伯数字转换为大写

将人民币中的阿拉伯数字转换为大写

/**-----------人民币 大写转换
 *
 * @param num ----
要转化的数字
 * @return
 */
public static String makeUpperCaseSum(String numStr) {
 
 String isNeg = "";//
正或者负
 double num = Double.parseDouble(numStr);
    if (num < 0) {
        num *= -1;
        isNeg = "
";
        numStr = numStr.substring(1);
    }
    final String[] upper_number = { "
","", "", "", "", "", "", "", "", "" };
    final String[] number_dw = { "", "
", "", "", "", "", "",
            "
", "亿", "", "", "", "" };
    String operate = numStr;
    String upper_str = "";
    String upper_str_pre = "";
    String upper_str_later = "";
    String vsDx;
    int i, j, point_pos, int_len;
    if (num < 0.01) {
        return "
零圆整";
    }

    upper_str_pre = "";


    point_pos = operate.indexOf(".");
    if (point_pos == -1) {
        int_len = operate.length();
    } else {
        int_len = point_pos;
    }
    if (int_len > number_dw.length) {
        return "too long
!!!";
    }

    if (num > 0) {
        for (i = 0; i < operate.length(); i++) {
            j = int_len - i;
            if(j>0){//
整数部分
                int k = Integer.parseInt(operate.substring(i, i + 1));
                if(k==0){//
数字为0
                 if(j==5||j==9)//
单位为亿或者万时
                     upper_str_pre += upper_number[k] + number_dw[j-1];
                    else                
                  upper_str_pre +=  upper_number[k];
                }
                else
                 upper_str_pre += upper_number[k] + number_dw[j-1];             
            }else if(j<0){//
小数部分
                int k = Integer.parseInt(operate.substring(i, i + 1));
                upper_str_later += upper_number[k]; 
                if(j==-1)
                    upper_str_later += "
";
                else
                    upper_str_later += "
";
            }
        } //end for
       
    } //end if
 upper_str_pre += "
";
    while(upper_str_pre.indexOf("
零零")!=-1){
     upper_str_pre = upper_str_pre.replaceAll("
零零","");
    }
 upper_str_pre = upper_str_pre.replaceAll("
零万","");
 upper_str_pre = upper_str_pre.replaceAll("
零亿","亿");
 upper_str_pre = upper_str_pre.replaceAll("
亿万","亿零");
 upper_str_pre = upper_str_pre.replaceAll("
零零","");
 upper_str_pre = upper_str_pre.replaceAll("
零圆","");
 if("
".equals(upper_str_pre))
  upper_str_pre = "";
 upper_str = isNeg+upper_str_pre + upper_str_later;
 upper_str = upper_str.replaceAll("
零角零分","");
 if(point_pos == -1)
  upper_str += "
";
 
    return upper_str;
}

 sql function实现:

create or replace function changeToCap(number_string varchar2,pos number) return varchar2 is
  Result varchar2(100);
  i number;
begin
  i:=1;
  if pos<=length(number_string) then
    if substr(number_string,length(number_string)-pos+1,1)='0' then
      Result:='零';
    elsif substr(number_string,length(number_string)-pos+1,1)='1' then
      Result:='壹';
    elsif substr(number_string,length(number_string)-pos+1,1)='2' then
      Result:='贰';
    elsif substr(number_string,length(number_string)-pos+1,1)='3' then
      Result:='叁';
    elsif substr(number_string,length(number_string)-pos+1,1)='4' then
      Result:='肆';
    elsif substr(number_string,length(number_string)-pos+1,1)='5' then
      Result:='伍';
    elsif substr(number_string,length(number_string)-pos+1,1)='6' then
      Result:='陆';
    elsif substr(number_string,length(number_string)-pos+1,1)='7' then
      Result:='柒';
    elsif substr(number_string,length(number_string)-pos+1,1)='8' then
      Result:='捌';
    elsif substr(number_string,length(number_string)-pos+1,1)='9' then
      Result:='玖';
    end if;
  else
      Result:='';
  end if;
  return(Result);
end changeToCap;

应用function:

select  ttc.tujian_total_cost,
changetocap(substr(ttc.tujian_total_cost,0,instr(ttc.tujian_total_cost,'.')-1),6) as shiwan,
changetocap(substr(ttc.tujian_total_cost,0,instr(ttc.tujian_total_cost,'.')-1),5) as wan,
changetocap(substr(ttc.tujian_total_cost,0,instr(ttc.tujian_total_cost,'.')-1),4) as qian,
changetocap(substr(ttc.tujian_total_cost,0,instr(ttc.tujian_total_cost,'.')-1),3) as bai,
changetocap(substr(ttc.tujian_total_cost,0,instr(ttc.tujian_total_cost,'.')-1),2) as shi,
changetocap(substr(ttc.tujian_total_cost,0,instr(ttc.tujian_total_cost,'.')-1),1) as ge,
'¥'||substr(ttc.tujian_total_cost,0,instr(ttc.tujian_total_cost,'.')-1) as lowercase
from T_TUJIAN_CHARGE ttc where ttc.modify_flag<>'D' and ttc.charge_id='100000023'

//中文大写金额转换函数,包括币制单位

CREATE OR REPLACE Function changeUppMoney(numMoney in Number)
        RETURN VARCHAR2
        IS strMoney VARCHAR2(60);
        strSign varchar2(4);
        numValue number(22);
        strNum Varchar2(2);
        strBit Varchar2(2);
        numBit number(2);
        numMod Number(1);
        strUppNum VARCHAR2(20):='零壹贰叁肆伍陆柒捌玖';
    strUppUnit VARCHAR2(34):='分角圆拾佰仟万拾佰仟亿拾佰仟万拾佰';
BEGIN
    if numMoney<0 then
                strSign:='[负]';
        else
                strSign:='';
        end if;
        numValue:=abs(numMoney)*100;
        strMoney:='';
        numBit:=0;
        LOOP
                numMod:=Mod(numValue,10);
        strNum:=substr(strUppNum,numMod+1,1);
        strBit:=substr(strUppUnit,numBit+1,1);
                strMoney:=strNum||strBit||strMoney;
                numValue:=FLOOR(numValue/10);
                numBit:=numBit+1;
                exit when numValue<1 or numBit>16;
        END LOOP;
        strMoney:=replace(strMoney,'零角','零');
        strMoney:=replace(strMoney,'零仟','零');
        strMoney:=replace(strMoney,'零佰','零');
        strMoney:=replace(strMoney,'零拾','零');
        strMoney:=replace(strMoney,'零零','零');
        strMoney:=replace(strMoney,'零零','零');
        strMoney:=replace(strMoney,'零分','整');
        strMoney:=replace(strMoney,'零圆','圆');
        strMoney:=replace(strMoney,'零万','万');
        strMoney:=replace(strMoney,'零亿','亿');
        strMoney:=replace(strMoney,'亿万','亿');
        strMoney:=strSign||strMoney;
        RETURN(strMoney);
END;

//中文大写数字转换函数,不包括币制单位,可指定空格填充,用于单据套打等场合:

CREATE OR REPLACE Function changeUppNumber(numMoney in Number,numSpace in Number)
        RETURN VARCHAR2
        IS strMoney VARCHAR2(200);
        strSign varchar2(4);
        numValue number(22);
        strNum Varchar2(20);
        numLen number(2);
        numBit number(2);
        numMod Number(1);
        strUppNum VARCHAR2(20):='零壹贰叁肆伍陆柒捌玖';
BEGIN
        if numSpace>10 then
                numLen:=12;
        else
                numLen:=numSpace+2;
        end if;
        if numMoney<0 then
                strSign:='[负]';
        else
                strSign:='';
        end if;
        numValue:=abs(numMoney)*100;
        strMoney:='';
        numBit:=0;
        LOOP
                numMod:=Mod(numValue,10);
        strNum:=lpad(substr(strUppNum,numMod+1,1),numLen);
                strMoney:=strNum||strMoney;
                numValue:=FLOOR(numValue/10);
                numBit:=numBit+1;
                exit when numValue<1 or numBit>16;
        END LOOP;
        strMoney:=strSign||strMoney;
        RETURN(strMoney);
END;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值