Solved

how to get if a negative number is in a range

Posted on 2014-09-12
3
78 Views
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

0
Comment
Question by:Johnny
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
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";
0
 

Author Closing Comment

by:Johnny
ID: 40320247
Thank you kind sir
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40320273
Thanks

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
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to count occurrences of each item in an array.

705 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

13 Experts available now in Live!

Get 1:1 Help Now