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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 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