本文介绍了将大量数字转换为英语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将小"数字转换为英文并不麻烦.但是,如果您处理BCMath任意精度数字,则可以.

Converting 'small' numbers to English is not to troublesome. But if you handle BCMath Arbitrary Precision numbers then it can be.

使用以下代码:

http://marc.info/?l=php- general& m = 99928281523866& w = 2

最大数量似乎是:

有人知道有一个转换大于该数字的函数吗?

Anyone know a function to convert numbers bigger than that?

推荐答案

您必须编写自己的函数,我建议使用数字作为字符串,并减去以下内容:

You have to write your own function, I suggest to use numbers as a string, let a substract like this:

    $string =  "12356";
    $text="";
    // level means 0-ones, 1- thousand , 2 million, 3 billion etc...
    $level=0;
    //until string has no character left
    while ($len=getval($string)){
      // get partial number from 0 to 999
      $string_partial = substr($string, (strlen($string)-$len)) ;
      // get hundreds
      $hund = ($string_partial - ($string_partial % 100))/100;
      // get tens
      $tens = $string_partial - ($hund *100);
      $tens = ($tens - ($tens %10))/10;
      // get ones
      $ones = $string_partial - ($tens*10) - ($hund*100);
      // remove partial_string form original string
      $string = substr($string, 0, (strlen($string)-$len));
      // edbug echoing
      echo $level . " - " . $hund. " - " . $tens .  " - " . $ones . "<br>";
      // you need to create a function that convert number to text only from 0 to 999 to set correct million/thousand etc, use $level.
      //$text = getTextvalue($hund,$tens,$ones,$level).$text;
      //increment $level
      $level++;
    }
    function getval($n){
      switch (strlen($n)){
       case 0: return false;
       case 1: return 1;
       case 2: return 2;
       default: return 3;
       }
    }

示例:

$string =  "123456789";

将输出

 $level - $hund - $tens - $ones
 0 - 7 - 8 - 9
 1 - 4 - 5 - 6  //thousand
 2 - 1 - 2 - 3  //million

这篇关于将大量数字转换为英语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 11:25