PHP number format

后端 未结 2 861
情书的邮戳
情书的邮戳 2020-11-29 11:52

I have this string :

000000000000100

and need to convert it to:

1,00

So, the rules are:

  1. Div
相关标签:
2条回答
  • 2020-11-29 12:15

    From the PHP Manual page on number_format:

    string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

    If you want numbers like 123456 be formatted as 1234,45, use:

    echo number_format($number / 100, 2, ",", "");
    

    If you need a dot as thousands separator (1.234,56):

    echo number_format($number / 100, 2, ",", ".");
    

    The zeros are automatically removed by PHP when converting the string to a number.

    0 讨论(0)
  • 2020-11-29 12:26
    string number_format ( float $number , 
                           int $decimals = 0 ,
                           string $dec_point = '.' ,
                           string $thousands_sep = ',' )
    

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

    // divide by 100 to shift ones place.
    echo number_format((int)'000000000000100' / 100,2,',','');
    
    0 讨论(0)
提交回复
热议问题