Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to avoid removing the last 0 of a floating number in the console?

Here is my code:

protected function roudUp(float $amount) : float {
    $fraction = self::ROUNDING_PRECISION > 0 ? self::ROUNDING_PRECISION : 0; 
    $mult = pow(10, $fraction);
    $roundedUp = ceil($amount * $mult) / $mult;
    return number_format($roundedUp, 2);
}

It works properly and returns always two decimal places like 1.60. But it converts to 1.6 in the console. See:
enter image description here

The expected result is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1.00
0.46
1.60
2.40
48.62

How can I avoid that?

>Solution :

number_format() returns a string. The function must also return a string and not a float!

function roudUp(float $amount) : string {
    $fraction = 2; 
    $mult = pow(10, $fraction);
    $roundedUp = ceil($amount * $mult) / $mult;
    return number_format($roundedUp, 2);
}

echo roudUp(1.6);  //1.60

https://3v4l.org/UWATr

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading