web123456

Arabic numerals to Chinese numerals (using PHP to achieve)

<?php //Arabic numerals to Chinese numerals function numberToChinese(int $num) { Positioning of the //section's position of authority $unit_section_pos = 0; $chn_str = ''; //Array for single digit conversion $chn_num_char = ["Zero.", "One.", "Two.", "Three.", "Four.", "Five.", "Six.", "Seven.", "Eight.", "Nine."]; //Array for conversion of section weights $chn_unit_section = ["", "Ten thousand.", "Billions.", "Trillions.", "Billions and billions."]; //Array of weight conversions within sections $chn_unit_char = ["", "Ten.", "Hundred.", "Thousand."]; $need_zero = false; if ($num === 0) { return $chn_num_char[0]; } //Closures for intra-section conversion $section_to_chinese = function ($section) use ($chn_num_char, $chn_unit_char) { $chn_str = ''; //Position within section $unit_pos = 0; $zero = true; while ($section > 0) { $v = $section % 10; if ($v === 0) { if (!$zero) { $zero = true; $chn_str = $chn_num_char[$v] . $chn_str; } } else { $zero = false; $str_ins = $chn_num_char[$v]; $str_ins .= $chn_unit_char[$unit_pos]; $chn_str = $str_ins . $chn_str; } $unit_pos++; $section = floor($section / 10); } return $chn_str; }; while ($num > 0) { $section = $num % 10000; if ($need_zero) { $chn_str = $chn_num_char[0] . $chn_str; } $str_ins = $section_to_chinese($section); $str_ins .= ($section !== 0) ? $chn_unit_section[$unit_section_pos] : $chn_unit_section[0]; $chn_str = $str_ins . $chn_str; $need_zero = ($section < 1000) && ($section > 0); $num = floor($num / 10000); $unit_section_pos++; } $search = 'Ten'; $replacement = 'Ten'; //Processing with a ten (this can be processed on demand) if (mb_substr($chn_str, 0, 2) === $search) { $position = strpos($chn_str, $search); $chn_str = substr_replace($chn_str, $replacement, $position, strlen($search)); } return $chn_str; } echo numberToChinese(1000001);