John Davidson

rounding - PHP calculation always round up

0 comments
Message:


So, I want to calculate two variables and multiply them to have an automatic price-per-unit-calculation. Everything works but as soon as I start calculating something, the result turns rounded off.


An example:


14,94 * 100 should be 1495
But I get 1400.


Tried number_format, round, ceil, etc. ....nothing resolved the issue.


That's the code, I already have $weight and $price as the currect numbers, so this isn't the problem.


$gewicht_angabe = strip_tags ($weight);
$preis_angabe = strip_tags ($price);
$ergebnis = number_format((($preis_angabe * 100) / $gewicht_angabe), 2);

I have PHP 7.4 on MariaDB , is there maybe something I'm missing?


EDIT:


Problem has been solved by @maik-lowrey . This is the fixed code that does what I want:


$gewicht_angabe = strip_tags ($weight);
$preis_angabe = strip_tags ($price);
$preis_angabe = str_replace(',', '.', $preis_angabe);
$ergebnis = number_format((($preis_angabe * 100) / $gewicht_angabe), 2);


My Answer:
It looks like the issue was with the format of the numbers being used in the calculation. By replacing any commas in the $price variable with periods, the calculation now works as expected. This ensures that the numbers are in the correct format for the calculation to be performed accurately.

Rate this post

3 of 5 based on 6829 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us