梨有套餐Numbers_Words :

$ numberToWord = new Numbers_Words();

echo $ numberToWords-> toWords(200);

这是我在大学里写的一个。 它还包括对负数的支持。 我知道有一些方法可以缩短和/或清理,但是,嘿,它适用于任何整数!

/** Converts an integer to its textual representation. @param num the number to convert to a textual representation @param depth the number of times this has been recursed */ function readNumber($num, $depth=0) { $num = (int)$num; $retval =""; if ($num < 0) // if it's any other negative, just flip it and call again return "negative " + readNumber(-$num, 0); if ($num > 99) // 100 and above { if ($num > 999) // 1000 and higher $retval .= readNumber($num/1000, $depth+3); $num %= 1000; // now we just need the last three digits if ($num > 99) // as long as the first digit is not zero $retval .= readNumber($num/100, 2)." hundred\n"; $retval .=readNumber($num%100, 1); // our last two digits } else // from 0 to 99 { $mod = floor($num / 10); if ($mod == 0) // ones place { if ($num == 1) $retval.="one"; else if ($num == 2) $retval.="two"; else if ($num == 3) $retval.="three"; else if ($num == 4) $retval.="four"; else if ($num == 5) $retval.="five"; else if ($num == 6) $retval.="six"; else if ($num == 7) $retval.="seven"; else if ($num == 8) $retval.="eight"; else if ($num == 9) $retval.="nine"; } else if ($mod == 1) // if there's a one in the ten's place { if ($num == 10) $retval.="ten"; else if ($num == 11) $retval.="eleven"; else if ($num == 12) $retval.="twelve"; else if ($num == 13) $retval.="thirteen"; else if ($num == 14) $retval.="fourteen"; else if ($num == 15) $retval.="fifteen"; else if ($num == 16) $retval.="sixteen"; else if ($num == 17) $retval.="seventeen"; else if ($num == 18) $retval.="eighteen"; else if ($num == 19) $retval.="nineteen"; } else // if there's a different number in the ten's place { if ($mod == 2) $retval.="twenty "; else if ($mod == 3) $retval.="thirty "; else if ($mod == 4) $retval.="forty "; else if ($mod == 5) $retval.="fifty "; else if ($mod == 6) $retval.="sixty "; else if ($mod == 7) $retval.="seventy "; else if ($mod == 8) $retval.="eighty "; else if ($mod == 9) $retval.="ninety "; if (($num % 10) != 0) { $retval = rtrim($retval); //get rid of space at end $retval .= "-"; } $retval.=readNumber($num % 10, 0); } } if ($num != 0) { if ($depth == 3) $retval.=" thousand\n"; else if ($depth == 6) $retval.=" million\n"; if ($depth == 9) $retval.=" billion\n"; } return $retval; }

不是很理想,但比“巨大的开关语句”要好得多:

$numbermappings = array("zero", "one","two","three", "four" .... "ninetynine"); echo $numbermappings[4]; // four

你仍然需要编写这个庞大的数组..

看到这个function在行动 :

function N2L($number) { $result = array(); $tens = floor($number / 10); $units = $number % 10; $words = array ( 'units' => array('', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eightteen', 'Nineteen'), 'tens' => array('', '', 'Twenty', 'Thirty', 'Fourty', 'Fifty', 'Sixty', 'Seventy', 'Eigthy', 'Ninety') ); if ($tens < 2) { $result[] = $words['units'][$tens * 10 + $units]; } else { $result[] = $words['tens'][$tens]; if ($units > 0) { $result[count($result) - 1] .= '-' . $words['units'][$units]; } } if (empty($result[0])) { $result[0] = 'Zero'; } return trim(implode(' ', $result)); }

有一个PEAR包可以做到这一点。 它的数字大于99,是多语言的,所以它可能比你需要的更重,但是仍然值得一试:

在面试过程中,我最终不得不写这个编码testing。 你可以在Github看到我的最终代码: https : //github.com/mangs/integers2words

为了方便起见,下面是DemoLibrary类,它实现了这个int2str()function(所有类成员都只支持int2str()function):

<?php /** * Demo library class intended to be added to in the future */ class DemoLibrary { /***** NOTE: a const cannot be an array in PHP, so making these arrays static is the next best thing *****/ /** * @var array $_numbersUnder20 Array containing the word associated with the index's number value */ private static $_numbersUnder20 = [ 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' ]; /** * @var array $_tensDigits Array containing all tens digit values except 10 */ private static $_tensDigits = [ 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' ]; /** * @var array $_orderOfMagnitude Array containing the higher-order digit values; can also be * thought of as the order of magnitude of the target digit */ private static $_orderOfMagnitude = [ // Stopped at "quintillion" because the maximum PHP int value on 64-bit Linux is // 9,223,372,036,854,775,807 (aka 2^63 - 1 because PHP doesn't support unsigned ints) 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion' ]; /** * Recursively calculates the string-, word-based equivalent of the target integer * * @param integer $num Integer whose value will be converted to a word-based string * @param boolean $recursive Determines if the currently-executing code is being called * recursively; allows for non-recursive 0 to be converted to "zero" * otherwise use an empty string * * @throws InvalidArgumentException if the first argument is not of type int * * @return string Partially- or fully-built word-based representation of the target integer */ private function _integerToWords($num, $recursive=false) { // Ensure a valid integer if(!is_int($num)) { throw new InvalidArgumentException( __FUNCTION__ . ' expects parameter 1 to be of type integer; actual type: ' . gettype($num) ); } /***** Perform the int to string conversion based on the size of $num *****/ // Negative if($num < 0) { return 'negative ' . $this->_integerToWords(-1 * $num, true); } // 0 or no value in the lowest digits if($num === 0) { return $recursive ? '' : 'zero'; } // 1-19 if($num < 20) { return self::$_numbersUnder20[$num]; } // 20 - 99 if($num < 100) { $highDigitValue = intval(floor($num / 10) - 2); // Value of the highest-order digit $remainingValue = $num % 10; // Value of the remaining digits return self::$_tensDigits[$highDigitValue] . '-' . $this->_integerToWords($remainingValue, true); } // 100 - 999 if($num < 1000) { $highDigitValue = intval(floor($num / 100)); // Value of the highest-order digit $remainingValue = $num % 100; // Value of the remaining digits return $this->_integerToWords($highDigitValue, true) . '-hundred ' . $this->_integerToWords($remainingValue, true); } // 1,000+ $quotient = $num; $divideCount = 0; while($quotient >= 1000) { $quotient /= 1000; ++$divideCount; } $highDigitValue = intval(floor($quotient)); // Value of the highest-order digit $remainingValue = $num - ($highDigitValue * pow(1000, $divideCount)); // Value of the remaining digits return $this->_integerToWords($highDigitValue, true) . '-' . self::$_orderOfMagnitude[$divideCount - 1] . ' ' . $this->_integerToWords($remainingValue, true); } /** * @api * * Calculates the string-, word-based equivalent of the target integer * * @param integer $num Integer whose value will be converted to a word-based string * * @return string Fully-built word-based representation of the target integer */ public function int2str($num) { return trim($this->_integerToWords($num), "- \t\n\r\0\x0B"); } }

php 转换成string,在PHP中将数字(1,2,3)转换为string(一,二,三)相关推荐

  1. oracle转换成字符型,Oracle中将Clob字段转换成字符串

    1. 利用dbms_lob.substr()方法可将对应字段转换成字符串如下 select dbms_lob.substr(content) from NEWS 该方法有个缺点,当content字段长 ...

  2. java 数字转换成汉字_Java-String-阿拉伯数字转换成中文汉字数字

    package com.test; public class NumberUtils { private static final String[] UNITS = { "", & ...

  3. java怎么把毫秒转换成天数_关于java:如何将毫秒转换为“ hh:mm:ss”格式?

    我糊涂了. 绊倒这个线程后,我试图找出如何格式化具有hh:mm:ss格式的倒数计时器. 这是我的尝试- //hh:mm:ss String.format("%02d:%02d:%02d&qu ...

  4. JAVA常用API或编程工具001---ITEXT把html转换成pdf的jar包,使用Java将HTML转换为PDF

    iText "XML Worker"允许开发人员以一种程序员友好的方式将XML文件转换成PDF文件.iText还可以将包含CSS样式的HTML转换为PDF格式的文档. 目标: 实现 ...

  5. 用python将十进制数转换成二进制数_python中的数据结构-将十进制数转换为二进制数...

    二进制表示法在计算机科学中很重要,计算机中存储的所有值都以一串二进制数字,即0和1的形式存在. 如果无法在通用表示形式和二进制数字之间来回转换,我们将需要以非常奇怪的方式与计算机进行交互. 十进制的数 ...

  6. 后缀为php的怎样转换成m3u8,【过程】第一次将m3u8文件转换为MP4文件经验分享

    因为工作原因,要将之前{中国影视童星大赛}的网络上的回放保存到本地下来,是微信里面的网页,用电脑打开的话,不会有下载按钮, 手机用UC浏览器看视频一般都会有下载按钮, 索性我就用uc下载到手机上再传到 ...

  7. java接收的文件转换成临时文件,java实现酷狗音乐临时缓存文件转换为MP3文件的方法...

    这篇文章主要介绍了java实现酷狗音乐临时缓存文件转换为MP3文件的方法,涉及java针对文件操作的相关技巧,需要的朋友可以参考下 本文实例讲述了java实现酷狗音乐临时缓存文件转换为MP3文件的方法 ...

  8. php数字转换汉字,如何在php中将数字转换成汉字

    将php数字转换为汉字的实现方法:首先,创建一个PHP代码样本文件:然后定义一个"数字2中国"的方法:然后,通过方法体中的开关循环语句实现转换逻辑:最后,执行文件. 推荐:PHP教 ...

  9. php 字母转换成小写字母,PHP中将大写字母转换为小写字母的函数是_________

    中字母式的下列可以转换把W文件为M件是E格格式音频的软. 写字小写数S学号询结学号课程执行组数目是包含表1句:绩W号分果中关系L语M成0查成绩分数的元. 母转斯认系毕达为天体的哥拉运行与数有关.:哪商 ...

最新文章

  1. SAP MM 如果存在OPEN的盘点凭证,则不能再次创建盘点凭证
  2. centos7安装mariadb
  3. linux 下一个 osw先从操作系统和标准脚本主动发起
  4. VTK:等参细胞演示用法实战
  5. python自定义全局异常_flask中主动抛出异常及统一异常处理代码示例
  6. [原创]Retrofit使用教程(二)
  7. 《怎样成为一个高手》观后感
  8. mediawiki禁止注册
  9. 带C#示例的String.Equality(==)运算符
  10. pycharm和pythonIDE安装详解
  11. linux read phy reg,请问如后配置嵌入式网卡LAN91C11X系列的自动协商模式(Auto-Negotiation)?...
  12. 计算机逻辑学包含分析,逻辑学在职研究生教育的基本内容分析
  13. YOLOV4论文记录
  14. Android常用库整理
  15. ubuntu20.04离线安装rabbitvcs
  16. 大部分码农辛苦半辈子,还是做不了软件架构师,转行后只能碌碌无为一生?(这是一个交流贴欢迎讨论)
  17. 微信公众号开发之网页授权
  18. Codeforces 1096D
  19. [WPF]图片裁切功能(鼠标绘制)
  20. android cad插件下载,CAD看图大师下载

热门文章

  1. 【JUC 并发编程】JUC 基本概念
  2. mysql时间戳里取小时
  3. 基于Eclipse和Mysql写的公交管理系统
  4. 【Noip模拟 20161004】局域网
  5. 收藏本站——添加到浏览器收藏夹
  6. 2022年危险化学品经营单位主要负责人最新解析及危险化学品经营单位主要负责人考试资料
  7. codeforces 711C Coloring Trees(DP)
  8. Java大端字节和小端字节
  9. kong安装启动问题
  10. 数据预处理之One-Hot(独热编码)编码