PHP金錢數字轉金錢大寫

/*
* 數字金額轉換成中文大寫金額的函數
* String Int $num 要轉換的小寫數字或小寫字符串
* return 大寫數字
*/
function num_to_rmb($num){
  $c1 = "零壹貳叄肆伍陸柒捌玖";
  $c2 = "分角元拾佰仟萬拾佰仟億";
  //精確到分後面就不要了,因此只留兩個小數位
  $num = round($num, 2); 
  //將數字轉化爲整數
  $num = $num * 100;
  if (strlen($num) > 10) {
    return "金額太大,請檢查";
  } 
  $i = 0;
  $c = "";
  while (1) {
    if ($i == 0) {
      //獲取最後一位數字
      $n = substr($num, strlen($num)-1, 1);
    } else {
      $n = $num % 10;
    }
    //每次將最後一位數字轉化爲中文
    $p1 = substr($c1, 3 * $n, 3);
    $p2 = substr($c2, 3 * $i, 3);
    if ($n != '0' || ($n == '0' && ($p2 == '' || $p2 == '' || $p2 == ''))) {
      $c = $p1 . $p2 . $c;
    } else {
      $c = $p1 . $c;
    }
    $i = $i + 1;
    //去掉數字最後一位了
    $num = $num / 10;
    $num = (int)$num;
    //結束循環
    if ($num == 0) {
      break;
    } 
  }
  $j = 0;
  $slen = strlen($c);
  while ($j < $slen) {
    //utf8一個漢字至關3個字符
    $m = substr($c, $j, 6);
    //處理數字中不少0的狀況,每次循環去掉一個漢字「零」
    if ($m == '零元' || $m == '零萬' || $m == '零億' || $m == '零零') {
      $left = substr($c, 0, $j);
      $right = substr($c, $j + 3);
      $c = $left . $right;
      $j = $j-3;
      $slen = $slen-3;
    } 
    $j = $j + 3;
  } 
  //這個是爲了去掉相似23.0中最後一個「零」字
  if (substr($c, strlen($c)-3, 3) == '') {
    $c = substr($c, 0, strlen($c)-3);
  }
  //將處理的漢字加上「整」
  if (empty($c)) {
    return "零元整";
  }else{
    return $c . "";
  }
}

 $money = 68000000.00;
 echo num_to_rmb($money); //陸仟捌佰萬元整函數

相關文章
相關標籤/搜索