Rounding negative

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Rounding negative

Post by spacebiscuit »

Hi guys,

I'm not sure if such a functio exists - but is it possible to have a number rounded up to 0 if and only if it is negative?

Thanks in advance,

Rob.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Rounding negative

Post by PHPHorizons »

Whereas, if the number were positive, it would just leave the number unchanged? I don't believe so.
User avatar
ColonelSandersLite
Forum Commoner
Posts: 35
Joined: Sun May 09, 2010 1:32 am

Re: Rounding negative

Post by ColonelSandersLite »

Simple:

Code: Select all

if ($number < 0) {$number = 0;};
You could even make a custom function to do it if you so pleased, though it may not be worth the effort, because this isn't something you'll need to do a lot really.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Rounding negative

Post by Weirdan »

Code: Select all

$number = max($number, 0);
Post Reply