phpnumbersformatmoney-format

Decimal money format


how to convert all numbers to xx,xx decimal format without exploding number or string? like;

8,9 --> 8,90

8,99 --> 8,99

12,1 --> 12,10

129,9 --> 129,90

any idea?


Solution

  • You can use number_format like:

    $n = 2.1;
    echo number_format($n, 2, ','); // 2,10
    

    If you have commas as decimal separators in your input you can convert values to float with:

    $number = floatval(str_replace(',', '.', str_replace('.', '', $string_number)));
    

    str_replace('.', '', $string_number) is used to remove thousand separators. str_replace(',', '.', ... ) is used to replace commas with dots.