how to get if a negative number is in a range

Posted on 2014-09-12
Last Modified: 2014-09-12
i have a number that will sometimes fall below 0 (negative) how do i check for that give like this please
// $percent_color=getColor(-0.4);
function getColor($n){
    // Return default (black) for all other numbers
    if($n==0) return "black";
    // Is number between 1 and 5?
    if($n>=0.1 && $n<=10) return "green";

    // Is number between 6 and 50?
    if($n>=11 && $n<=20) return "orange";

    // Is number greater than 50
    if($n>21) return "red";

    // negitive lookup
    // Is number between 1 and 5?
    if($n>=-0.1 && $n<=-10) return "green";

    // Is number between 6 and 50?
    if($n>=-11 && $n<=-20) return "orange";

    // Is number greater than 50
    if($n>-21) return "red";


Open in new window

Question by:Johnny
  • 2
LVL 15

Accepted Solution

ChloesDad earned 500 total points
ID: 40320232
This bit looks wrong, you have the logic the wrong way around. I have corrected

   // Is number between 1 and 5?
    if($n<0 && $n>=-10) return "green";

    // Is number between 6 and 50?
    if($n<-11 && $n>=-20) return "orange";

    // Is number greater than 50
    if($n<-20) return "red";

Author Closing Comment

ID: 40320247
Thank you kind sir
LVL 15

Expert Comment

ID: 40320273

this bit I edited wrong

// Is number between 6 and 50?
     if($n<-11 && $n>=-20) return "orange";

should be

// Is number between 6 and 50?
     if($n<-10 && $n>=-20) return "orange";

otherwise you will miss if the value is -11

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now