//
// chinese_number()
// PHP function to return the UTF-8 (Unicode) encoding of a Chinese/Japanese number
// Works for numbers 0 - 9999
//
// (C) 2008 Eric Shalov. Free for all to use without restriction.
//
// Usage: echo chinese_number(55);
//

  function chinese_number($n) {
    $chinese_digits = array(
      0 => "〇",
      1 => "一",
      2 => "二",
      3 => "三",
      4 => "四",
      5 => "五",
      6 => "六",
      7 => "七",
      8 => "八",
      9 => "九"
    );
    
    $s = "";
    $thousands = floor($n / 1000);
    $hundreds =  floor( ($n - $thousands*1000) / 100);
    $tens =      floor( ($n - $thousands*1000 - $hundreds*100) /  10);
    $ones =      floor(  $n - $thousands*1000 - $hundreds*100 - ($tens*10) );

    if($thousands > 1) $s .= $chinese_digits[$thousands];
    if($thousands > 0) $s .= "千"; // qian
    if($hundreds  > 1) $s .= $chinese_digits[$hundreds];
    if($hundreds  > 0) $s .= "百"; // bai
    if($tens      > 1) $s .= $chinese_digits[$tens];
    if($tens      > 0) $s .= "十"; // shi
    if($ones > 0 || $n == 0) $s .= $chinese_digits[$ones];
    
    return $s;
  }

Find an error or omission? Sorry about that! Please e-mail Eric at eric@ericshalov.com and let him know!

All of Eric's Tech Notes are provided on an as-is basis, and may contain errors or omissions. No statement is made as to thier suitability for any particular purpose, and no warranty is given. Use at your own risk! All trademarks are the property of their respective owners.
No duplication of the above information is permitted without prior written permission of the author(s).
©Copyright 2007 Eric Shalov. All Rights Reserved.