How do I print traditional Chinese numbers with PHP?

//
// 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;
  }

 

Share: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Twitter
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Reddit
  • StumbleUpon

Leave a Reply

Your email address will not be published. Required fields are marked *