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

Unnatural round prices

I need to round prices for my client in a non-common way :

120.28 => 120.2

130.23 => 130.2

150.64 => 150.6

The rule is to always keep the nearest lowest 1 precision.

I try to use the round() method but all my tries are useless since this method handle half which i have to ignore.

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

ceil() and floor() will not work out of the box.

Is a combination of those 3 methods would work ?

>Solution :

How about 0.1 * intval(10 * $price)?

What it does is multiply the price by 10, so 120.28 becomes 1202.8. Then it takes integer part using intval(), which is 1202, and then divides it by ten giving 120.2.

See: https://3v4l.org/3qMhd

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