|
發表者 |
討論內容 |
冷日 (冷日) |
發表時間:2010/3/20 9:46 |
- Webmaster

- 註冊日: 2008/2/19
- 來自:
- 發表數: 15771
|
- [轉貼]php下實現一個阿拉伯數字轉中文數字的函數
- 如果要用於金額的轉換,對小數部分的處理要做一下修改
<?php
function ch_num($num,$mode=true) {
$char = array("零","壹","貳","三","肆","伍","陸","柒","捌","玖");
$dw = array("","拾","佰","仟","","萬","億","兆");
$dec = "點";
$retval = "";
if($mode)
preg_match_all("/^0*(\d*)\.?(\d*)/",$num, $ar);
else
preg_match_all("/(\d*)\.?(\d*)/",$num, $ar);
if($ar[2][0] != "")
$retval = $dec . ch_num($ar[2][0],false); //如果有小數,先遞歸處理小數
if($ar[1][0] != "") {
$str = strrev($ar[1][0]);
for($i=0;$i<strlen($str);$i++) {
$out[$i] = $char[$str[$i]];
if($mode) {
$out[$i] .= $str[$i] != "0"? $dw[$i%4] : "";
if($str[$i]+$str[$i-1] == 0)
$out[$i] = "";
if($i%4 == 0)
$out[$i] .= $dw[4+floor($i/4)];
}
}
$retval = join("",array_reverse($out)) . $retval;
}
return $retval;
}
//echo ch_num("12345006789001.123");
//echo ch_num("880079.1234");
echo ch_num("300045.0123");
?>
详细出处参考:http://www.jb51.net/article/15111.htm
|
|
冷日 (冷日) |
發表時間:2010/3/20 9:49 |
- Webmaster

- 註冊日: 2008/2/19
- 來自:
- 發表數: 15771
|
- [轉貼]PHP转换大写中文数字
- 轉換整數、浮點數為大寫中文數字:
<?php
/**
* Transform arabic numerals into chinese figures
*
* @author Lenin Lee
* @copyright Copyright (c) 2008, Lenin Lee
* @lisense http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Lisense
* @version 1.0
*/
class NumPP{
/**
* All single chinese figures
*
* @var array
*/
var $chFigure = array("零", "壹", "貳", "三", "肆", "伍", "陸", "柒", "捌", "玖");
/**
* Small orders of magnitude
*
* @var array
*/
var $chSmallUnit = array("拾", "佰", "仟");
/**
* Big orders of magnitude
*
* @var array
*/
var $chBigUnit = array("萬", "億");
/**
* Constructor
*/
function NumPP(){
}
/**
* The main method by which an arabic numeral is converted into a chinese figure
*/
function arabic2chinese($num){
$chFgr = ''; //The unsigned chinese figure converted by this method
$sign = ''; //The sign of chinese figure
$intPart = '';
$decPart = '';
// This class does not validate the number given , but converts it into a string directly
$num = strval($num);
// Get the sign
if (substr($num, 0, 1) == '-'){
$sign = '負';
$num = substr($num, 1);
}
// Split a floating number into two parts , integer part and decimal part
$numInfo = explode(".", $num);
$intPart = $numInfo[0];
$decPart = $numInfo[1];
// Transform the integer part
$tmp = $intPart;
$flagBigUnit = -1; //Indicate which big unit is going to be used
while (strlen($tmp)){
$seg = substr($tmp, - (strlen($tmp) <4 ? strlen($tmp): 4)); //Get a new segment from the end to the begining
$chSeg = $this -> _parseSeg($seg);
$chSegWithBigUnit = $chSeg . (strlen($chSeg) == 0 ? '' : $this -> chBigUnit[$flagBigUnit]);
//Deside which big unit is to be used here
$withZero = false;
//Deside whether a chinese figure 0 should be added by the end of the segment that was just transformed
if (substr($seg, -1) == '0' && strlen($chFgr) != 0 && substr($chFgr, 0, 2) != '零'){
$withZero = true;
}
$chFgr = $chSegWithBigUnit . ($withZero ? '零' : '') . $chFgr;
$flagBigUnit = $flagBigUnit == -1 ? 0 : ($flagBigUnit == 0 ? 1 : 0);
//Switch between the two big units
$tmp = substr($tmp, 0, - (strlen($tmp) <4 ? strlen($tmp) : 4));
//Truncate the integer part
}
// In case that the integer part is zero
if (strlen($chFgr) == 0){
$chFgr = '零';
}
// Transform the decimal part
if (strlen($decPart)> 0){
$chFgr .= '點';
for($i = 0;$i <strlen($decPart);$i++){
$chFgr .= $this -> chFigure[$decPart[$i]];
}
}
return $sign . $chFgr;
}
/**
* A big integer will be split into some segments with a length of 4 bits or less ,
* this method transforms every segment into a chinese figure
*/
function _parseSeg($seg){
$chSeg = '';
$len = strlen($seg);
$formerIsZero = false; //Mark if the former bit is zero
for($i = 0;$i <$len;$i++){
if (substr($seg, -1) == '0'){ // If the current bit is zero ...
if ($i != 0){ // If the current bit is not in the unit position ...
$chSeg = $formerIsZero ? $chSeg : '零' . $chSeg;
}
$formerIsZero = true;
}else{ // If the current bit is not zero ...
$chSeg = $this -> chFigure[substr($seg, -1)] . $this -> chSmallUnit[$i-1] . $chSeg;
$formerIsZero = false;
}
$seg = substr($seg, 0, -1);
}
return $chSeg;
}
}
?>
實例:
<?php
include_once 'numpp.php';
$npp = new NumPP();
echo $npp->arabic2chinese('10081034500.0076');
?>
結果:
壹佰億零捌仟壹佰零三萬肆仟伍佰點零零柒陸
原文出處:【牧码志】 » Blog Archive » PHP转换大写中文数字
|
|
冷日 (冷日) |
發表時間:2010/3/20 9:50 |
- Webmaster

- 註冊日: 2008/2/19
- 來自:
- 發表數: 15771
|
- [轉貼]php数字转换成中文数字教程
- php數字轉換成中文數字教程
class ChineseNumber
{
var $basical=array(0=>"零","一","二","三","四","五","六","七","八","九");
//var $basical=array(0=>"零","壹","貳","三","肆","伍","陸","柒","捌","玖");
var $advanced=array(1=>"十","百","千");
//var $advanced=array(1=>"拾","佰","仟");
var $top=array(1=>"萬","億");
var $level; // 以4位為一級
// 先實現萬一下的數的轉換
function ParseNumber($number)
{
if ($number>999999999999) // 只能處理到千億。
return "數字太大,無法處理。抱歉!";
if ($number==0)
return "零";
for($this->level=0;$number>0.0001;$this->level++,$number=floor($number / 10000))
{
// 對於中文來說,應該是4位為一組。
// 四個變量分別對應 個、十、百、千 位。
$n1=substr($number,-1,1);
if($number>9)
$n2=substr($number,-2,1);
else
$n2=0;
if($number>99)
$n3=substr($number,-3,1);
else
$n3=0;
if($number>999)
$n4=substr($number,-4,1);
else
$n4=0;
if($n4)
$parsed[$this->level].=$this->basical[$n4].$this->advanced[3];
else
if(($number/10000)>=1) // 千位為0,數值大於9999的情況
$parsed[$this->level].="零";
if($n3)
$parsed[$this->level].=$this->basical[$n3].$this->advanced[2];
else
if(!ereg("零$",$parsed[$this->level]) && ($number / 1000)>=1) // 不出現連續兩個「零」的情況
$parsed[$this->level].="零";
if($n2)
$parsed[$this->level].=$this->basical[$n2].$this->advanced[1];
else
if(!ereg("零$",$parsed[$this->level]) && ($number / 100)>=1) // 不出現連續兩個「零」的情況
$parsed[$this->level].="零";
if($n1)
$parsed[$this->level].=$this->basical[$n1];
}
for($this->level-=1;$this->level>=0;$this->level--)
{
$result.=$parsed[$this->level].$this->top[$this->level];
}
if(ereg("零$",$result))
$result=substr($result,0,strlen($result)-2);
return $result;
}
};
$c=new ChineseNumber();
echo $c->ParseNumber(123456789012);
本文来自:日光月美(www.xssep.cn) 原文链接:http://www.xssep.cn/view-137.html
|
|
|
|