将大量数字转换为英语口语

发布于 2024-10-17 03:10:49 字数 333 浏览 2 评论 0原文

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

使用以下代码:

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

最大数量似乎是:

二十亿一百四十七 万四百八十三 千六百四十七

有人知道转换比这更大的数字的函数吗?

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

Using code from:

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

The maximum number seems to be:

two billion one hundred forty seven
million four hundred eighty three
thousand six hundred forty seven

Anyone know a function to convert numbers bigger than that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栖迟 2024-10-24 03:10:50

你必须编写自己的函数,我建议使用数字作为字符串,让像这样的减法:

    $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

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

example:

$string =  "123456789";

will output

 $level - $hund - $tens - $ones
 0 - 7 - 8 - 9  
 1 - 4 - 5 - 6  //thousand
 2 - 1 - 2 - 3  //million
乙白 2024-10-24 03:10:50

对于大于 PHP_MAX_INT 的数字,您必须依赖外部服务编写函数,例如 WolframAlpha

PHP 为此提供的唯一功能是 Intl 的 NumberFormatterNumberFormatter::format 接受浮点数,但这意味着它只能处理 64 位长架构中最大 2^63-1 的整数或 32 位长架构中的 52 位数字。

For numbers larger than PHP_MAX_INT, you'll have to either write the function on rely on an external service, such as WolframAlpha.

The only functionality PHP offers for this is Intl's NumberFormatter. NumberFormatter::format accepts floats, but this means it can only handle integers up to 2^63-1 in 64-bit long architectures or 52-bit numbers in 32-bit ones.

樱桃奶球 2024-10-24 03:10:50

我编写了 vpi2english 函数来处理大至 10^306 - 1 的整数。它是我的 VPI 工具箱

vpi2english(vpi('12000000110022987')) 
ans = 
twelve quadrillion, one hundred ten million, twenty two thousand, nine hundred eighty seven

I wrote the vpi2english function to handle integers as large as 10^306 - 1. It is part of my VPI toolbox.

vpi2english(vpi('12000000110022987')) 
ans = 
twelve quadrillion, one hundred ten million, twenty two thousand, nine hundred eighty seven
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文