How to lease value without pennies down?

Asked

Viewed 133 times

4

How to round values that have no pennies and only have numbers?

Ex: 10, 20, 30, 40, 50. If person is 13, stay 10, if 25 becomes 20.

  • What you’ve already done?

  • is always round down?

  • @Fleuquerlima yes, the bigown has already helped me. Thank you all.

1 answer

4


Divide by 10 and use the function intval() to take the whole part, it will lose the "broken" part, then multiply by 10 again to re-establish the magnitude.

If you need to round up normally add up to 5 or another number, it depends on the intention. But this can vary.

If you need to round two boxes, the size should be 100 instead of 10.

You can create a function to generalize this.

echo intval(13 / 10) * 10 . "\n";
echo intval(25 / 10) * 10 . "\n";
echo intval(137 / 10) * 10 . "\n";

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

There is also the ready function round() which does this. The decimal parameter can be negative, this means that the boxes are to the left of the comma, as you want. But she doesn’t guarantee it’ll round down.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.