PHP division adds a comma

I'm trying to convert cents to dollars (I don't need dollar sign, just value) but when I divide a number smaller than 100 by 100 I get a strange result.

Eg.: 1/100 give me 0,01.0

I don't need that comma, I need 0.01 as it should be.

I also tried number_format but it returns a string and when I cast the result to float I get the same strange value.

How can I fix it?

Thanks in advance for your help

This is the function I'm using:

public static function convertFromCents($value) {
    if(is_numeric($value)) {
        $value = $value/100;
    } else {
        $value = 0;

    return $value;

This are the proof of what I'm saying: enter image description here

enter image description here

1 answer

  • answered 2021-01-25 17:03 guyaloni

    It looks like what you see are messages of your IDE, not the real value of your variable.

    In order to achieve what you want, you can simply use round function.
    The second parameter is precision, which determines how many digits will appear after the point:

    // return 0.33333333333333
    echo 1/3;
    // return 0.33
    echo round(1/3, 2);