【问题标题】:How to split long numbers by commas to readable number using php?如何使用 php 将长数字通过逗号拆分为可读数字?
【发布时间】:2026-01-29 14:35:01
【问题描述】:

我正在从我的数据库中获取价格。它正在使用中显示。 它显示价格如下:5000000

如何拆分它以显示类似5,000,000的内容

【问题讨论】:

标签: php string text numbers


【解决方案1】:

你试过the manual吗?

number_format 完全符合您的要求:

<?php
$number = 5000000;
echo number_format($number); //output: 5,000,000

【讨论】:

    【解决方案2】:

    使用money_format(string,number)

    (货币格式不适合 windows)

    money_format

    也使用`number_format()

    number_format — 用千位分组格式化一个数字

    &lt;?php echo number_format("5000000")."&lt;br&gt;"; ?&gt;

    number_format

    【讨论】:

    • 它应该是noted:money_format 在 windows 系统上不可用 - 使用它之前必须正确设置区域设置。
    【解决方案3】:

    你需要为此使用php内置函数number_format

    echo number_format($price);
    

    【讨论】:

      【解决方案4】:
      <?php
      
      $number = 1234.56;
      
      // let's print the international format for the en_US locale
      setlocale(LC_MONETARY, 'en_US');
      echo money_format('%i', $number) . "\n";
      // USD 1,234.56
      
      // Italian national format with 2 decimals`
      setlocale(LC_MONETARY, 'it_IT');
      echo money_format('%.2n', $number) . "\n";
      // Eu 1.234,56
      
      // Using a negative number
      $number = -1234.5672;
      
      // US national format, using () for negative numbers
      // and 10 digits for left precision
      setlocale(LC_MONETARY, 'en_US');
      echo money_format('%(#10n', $number) . "\n";
      // ($        1,234.57)
      
      // Similar format as above, adding the use of 2 digits of right
      // precision and '*' as a fill character
      echo money_format('%=*(#10.2n', $number) . "\n";
      // ($********1,234.57)
      
      // Let's justify to the left, with 14 positions of width, 8 digits of
      // left precision, 2 of right precision, withouth grouping character
      // and using the international format for the de_DE locale.
      setlocale(LC_MONETARY, 'de_DE');
      echo money_format('%=*^-14#8.2i', 1234.56) . "\n";
      // Eu 1234,56****
      
      // Let's add some blurb before and after the conversion specification
      setlocale(LC_MONETARY, 'en_GB');
      $fmt = 'The final value is %i (after a 10%% discount)';
      echo money_format($fmt, 1234.56) . "\n";
      // The final value is  GBP 1,234.56 (after a 10% discount)
      
      ?>
      

      【讨论】:

      • 最好也添加参考网址。
      • 简单地从另一个页面复制代码而没有自己解释或对源代码的任何引用是不是很好。你可以说你是从the manual得到的