PHP float with 2 decimal places: .00

后端 未结 11 1030
野性不改
野性不改 2020-12-08 06:34

When I do this typecasting:

(float) \'0.00\';

I get 0. How do I get 0.00 and still have the data type as a float?

相关标签:
11条回答
  • 2020-12-08 07:06

    When we format any float value, that means we are changing its data type to string. So when we apply the formatting on any amount/float value then it will set with all possible notations like dot, comma, etc. For example

    (float)0.00 => (string)'0.00',
    (float)10000.56 => (string) '10,000.56'
    (float)5000000.20=> (string) '5,000,000.20'
    

    So, logically it's not possible to keep the float datatype after formatting.

    0 讨论(0)
  • 2020-12-08 07:07

    Use the number_format() function to change how a number is displayed. It will return a string, the type of the original variable is unaffected.

    0 讨论(0)
  • 2020-12-08 07:07

    You can use this simple function. number_format ()

        $num = 2214.56;
    
        // default english notation
        $english_format = number_format($num);
        // 2,215
    
        // French notation
        $format_francais = number_format($num, 2, ',', ' ');
        // 2 214,56
    
        $num1 = 2234.5688;
    
       // English notation with thousands separator
        $english_format_number = number_format($num1,2);
        // 2,234.57
    
        // english notation without thousands separator
        $english_format_number2 = number_format($num1, 2, '.', '');
        // 2234.57
    
    0 讨论(0)
  • 2020-12-08 07:08

    You can show float numbers

    • with a certain number of decimals
    • with a certain format (localised)

    i.e.

    $myNonFormatedFloat = 5678.9
    
    $myGermanNumber = number_format($myNonFormatedFloat, 2, ',', '.'); // -> 5.678,90
    
    $myAngloSaxonianNumber = number_format($myNonFormatedFloat, 2, '.', ','); // -> 5,678.90 
    

    Note that, the

    1st argument is the float number you would like to format

    2nd argument is the number of decimals

    3rd argument is the character used to visually separate the decimals

    4th argument is the character used to visually separate thousands

    0 讨论(0)
  • 2020-12-08 07:09

    try this

    $result = number_format($FloatNumber, 2);
    
    0 讨论(0)
提交回复
热议问题