Solved

[PHP] Select variable closest to 0? show me

Posted on 2004-09-28
8
305 Views
Last Modified: 2010-04-17
I have some code which contains two variables.

How can I check which one is closest to 0? My variables can be negative and positive hence why i need it to be 0 and work whatever the number is.

Could you show me an example of the code.
thanks
0
Comment
Question by:georgecooldude
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Accepted Solution

by:
DaveyEss earned 20 total points
ID: 12167676
You need to use the abs function in php

The following code gives an example of what to do.

<?php
$val1 = 7;
$val2 = -5;

echo "closer to zero ";

if (abs($val1) > abs($val2)) {
   echo $val2;
}
else {
   echo $val1;
}
?>
0
 
LVL 5

Author Comment

by:georgecooldude
ID: 12167779
DaveyEss,

I just tryed that but it came up with my postive value closest to 0 when infact I had a negative value closer.


3
2
1
0 - This being the center point
-1
-2
-3

Any ideas how i can do that? Maybe I need to take both values and divde by one and then check which is closest to 0? This would then work with your above code? Am I correct in thinking this?

If so could you show me how i could do that?
0
 
LVL 4

Expert Comment

by:DaveyEss
ID: 12167828
What are the positive and negative values that didn't work?
0
 
LVL 5

Author Comment

by:georgecooldude
ID: 12167871
They were

-22200
 24600

and it returned the result of 24600
0
How to run any project with ease

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

 
LVL 4

Expert Comment

by:DaveyEss
ID: 12167888
On my test, the following code returns -22200.  Does yours do that?  I have saved the file as a php web page (test.php) and then executed it in the browser.

The output I get is "closer to zero -22200"


<?php
$val1 = 24600;
$val2 = -22200;

echo "closer to zero ";

if (abs($val1) > abs($val2)) {
   echo $val2;
}
else {
   echo $val1;
}
?>
0
 

Expert Comment

by:Daniel_Iankov
ID: 12167978
Well the above statement should work, but if you say that the abs function is not working i suppose that
<?php
$val 1 = -3;
$val 2 = 5;

if($val1*$val2>0){  //same allinment
      if(($val1-$val2)*$val1 > 0){
           echo $val2;
      } else {
           echo $val 1;
      }

} else {
     if (($val1+$val2)*$val1 >0){
         echo $val2;
     } else {
         echo $val1;
     }
}  
?>
0
 
LVL 5

Author Comment

by:georgecooldude
ID: 12168054
My mistake.

I had assigned a variable two values by mistake in an include and it was making the figure larger than it should have been.

Thanks for your help.

And thanks Daniel :-) I only noticed your reply after I refreshed the page when I awarded the point.
0
 

Expert Comment

by:Daniel_Iankov
ID: 12168162
NP:)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to setup 18 80
VBA color chart bars 12 67
delphi parse string to params 3 83
C# DataGridView_RowsAdded event not firing 1 40
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

747 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

10 Experts available now in Live!

Get 1:1 Help Now