how to reformat a number with commas

前端 未结 4 1605
清酒与你
清酒与你 2020-12-20 02:43

I have a line of code that get a count of rows in my table and then display the count. In this case it display the number of downloads.

Right now the count is very s

4条回答
  •  执笔经年
    2020-12-20 03:01

    You can also use FORMAT(X, D) in your query:

    Formats the number X to a format like '#,###,###.##', rounded to D decimal places, and returns the result as a string. If D is 0, the result has no decimal point or fractional part.

    mysql> SELECT FORMAT(12332.123456, 4);

        -> '12,332.1235'
    

    mysql> SELECT FORMAT(12332.1,4);

        -> '12,332.1000'
    

    mysql> SELECT FORMAT(12332.2,0);

        -> '12,332'
    

    Edit:

    I was about to add that "the downside of using this method is that you cannot that number in your code", but apparently this is not a problem anymore, starting from PHP 5.3, thanks to the parse function of NumberFormatter class:

    NumberFormatter::parse numfmt_parse

    — Parse a number

    Example

    parse($num)."\n";
    echo $fmt->parse($num, NumberFormatter::TYPE_INT32)."\n";
    ?>
    

提交回复
热议问题