Convert a number to its string representation

前端 未结 4 1748
野性不改
野性不改 2021-01-13 21:53

I\'m developing a simple web application where in I need to display number a to my users in string format.

Example:

12 - One Two or Twelve
-20 - min         


        
4条回答
  •  猫巷女王i
    2021-01-13 22:22

    If you want to spell out the complete number you can make use of the PEAR Numbers_Words class. This class has a toWords() method that accepts a +ve or a -ve num and returns the spelled out string representation of the number.

    If you want to convert the number to string digit wise, I am not aware of any lib function. But you can code one yourself easily. user187291 gives a good way to do this in his answer.

    toWords($num)."\n";
    }    
    
    ?>
    

    Output:

    C:\>php a.php
    -12 = minus twelve
    20 = twenty
    

提交回复
热议问题