人民币数字转中文币制

来源:互联网 发布:php仿系统之家源码 编辑:程序博客网 时间:2024/06/03 09:25
 前段时间开发,遇到需求要把人民币数字转成中文币制显示。在网上找了一下没有,没有直接可以用的,于是就自己写了一下。格式举例:

0.01 壹分

1.02 壹元零贰分

100  壹佰元整

101  壹佰零壹元整

101.01 壹佰零壹元零壹分

101.21 壹佰零壹元贰角壹分

101.2 壹佰零壹元贰角

1000.01 壹仟元零壹分

1001.01 壹仟零壹元零壹分

1010 壹仟零壹拾元整


数字:壹

单位:万

function replaceNumberToCnCapitals($num){$capnum = array( "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" );$capdigit = array( "", "拾", "佰", "仟" );$subnum = explode( ".", $num );$yuan = $subnum[0];$j = 0;$nonzero = 0;for( $i=0; $i<strlen($subnum[0]); $i++ ){if( $i==0 ){ //确定个位$cncap = "元";    }if( $i==4 ){ //确定万位$j = 0;$nonzero = 0;$cncap = "万" . $cncap;    }if($i==8){ //确定亿位$j = 0;$nonzero = 0;$cncap = "亿" . $cncap;    }$tailnum = substr($yuan,-1,1); //截取尾数if(strlen($subnum[0])==1 && $subnum[0]==0){$cncap='';    }else{$cncap = ($tailnum) ? $capnum[$tailnum].$capdigit[$j].$cncap : (($nonzero) ? "零".$cncap : $cncap);    }$nonzero = ($tailnum) ? 1 : $nonzero;$yuan = substr($yuan,0,strlen($yuan)-1); //截去尾数$j++;  }$chiao = $cent = '';$float1 = isset($subnum[1]) ? (int)substr($subnum[1],0,1) : 0;$float2 = isset($subnum[1]) ? (int)substr($subnum[1],1,1) : 0;if(!empty($cncap)){//有整数位if($float1>0 && $float2>0){$chiao = (substr($subnum[1],0,1)) ? $capnum[substr($subnum[1],0,1)]."角" : "零";$cent = (substr($subnum[1],1,1)) ? $capnum[substr($subnum[1],1,1)]."分" : "零分";    }elseif($float1>0 && $float2==0){$chiao = (substr($subnum[1],0,1)) ? $capnum[substr($subnum[1],0,1)]."角" : "零";    }elseif($float1==0 && $float2>0){$chiao= (substr($subnum[1],0,1)) ? $capnum[substr($subnum[1],0,1)]."角" : "零";$cent= (substr($subnum[1],1,1)) ? $capnum[substr($subnum[1],1,1)]."分" : "零分";    }  }else{//无整数位(只有小数位)if($float1>0 && $float2>0){$chiao = $capnum[substr($subnum[1],0,1)]."角";$cent = $capnum[substr($subnum[1],1,1)]."分";    }elseif($float1>0 && $float2==0){$chiao = $capnum[substr($subnum[1],0,1)]."角";    }elseif($float1==0 && $float2>0){$cent= $capnum[substr($subnum[1],1,1)]."分";    }  }if(!empty($cncap) && empty($chiao) && empty($cent)){$cncap .= '整';  }else{$cncap .= $chiao.$cent;  }$cncap = preg_replace("/(零)+/","\\1",$cncap); //合并连续的零return $cncap;}



0 0
原创粉丝点击