How can I add commas to numbers in PHP

后端 未结 4 1789
难免孤独
难免孤独 2020-11-30 05:45

I would like to know how can I add comma\'s to numbers. To make my question simple.

I would like to change this:

1210 views

To:

相关标签:
4条回答
  • 2020-11-30 05:48

    The Following code is working for me, may be this is helpful to you.

    $number = 1234.56;
    
    echo number_format($number, 2, '.', ',');
    

    //1,234.56

    0 讨论(0)
  • 2020-11-30 05:49

    Often, if a number is big enough to have commas in it, you might want to do without any numbers after a decimal point - but if the value you are showing could ever be small, you would want to show those decimal places. Apply number_format conditionally, and you can use it to both add your commas and clip off any irrelevant post-point decimals.

    if($measurement1 > 999) {
        //Adds commas in thousands and drops anything after the decimal point
        $measurement1 = number_format($measurement1);
        }
    

    Works well if you are showing a calculated value derived from a real world input.

    0 讨论(0)
  • 2020-11-30 05:54

    from the php manual http://php.net/manual/en/function.number-format.php

    I'm assuming you want the english format.

    <?php
    
    $number = 1234.56;
    
    // english notation (default)
    $english_format_number = number_format($number);
    // 1,235
    
    // French notation
    $nombre_format_francais = number_format($number, 2, ',', ' ');
    // 1 234,56
    
    $number = 1234.5678;
    
    // english notation with a decimal point and without thousands seperator
    $english_format_number = number_format($number, 2, '.', '');
    // 1234.57
    
    ?>
    

    my 2 cents

    0 讨论(0)
  • 2020-11-30 06:05
     $number = 1234.56;
    
    //Vietnam notation(comma for decimal point, dot for thousand separator)
    
     $number_format_vietnam = number_format($number, 2, ',', '.');
    
    //1.234,56
    
    0 讨论(0)
提交回复
热议问题