Ria
Ria

Reputation: 526

How to stop decimals being rounded up or down using php

I am calculating a number which I use in another calculation. the problem is this number could be 67.9699 I want it to say 67.96 without rounding up or down just chop the other digits of. I was hoping the floor function would do this like:

$num2 = floor(($num1/5)*100)/100;

How ever this is still rounding the number up to 67.97 is there a away to stop this. Any help welcome

Upvotes: 1

Views: 1530

Answers (3)

izn
izn

Reputation: 310

It's working well for me, try this:

echo floor(67.9699 * 100) / 100;

Demo Online

By the way, number_format() rounds the number.

Upvotes: 3

peterdoesco.de
peterdoesco.de

Reputation: 529

You can use number_format();

number_format

Upvotes: 0

spod
spod

Reputation: 416

$num2 = substr($num2,0,5);
echo $num2; 

If you want to use the number again as float

$num2 = floatval($num2);

Upvotes: 0

Related Questions