Jump to content

Recommended Posts

if ($integer>=0.000)
{
$plusminus = '+';
$arrow = 'uparrow.png';
}
else
{
$plusminus = '-';
$arrow = 'downarrow.png';
}

 

The code above is not working correctly when dealing with numbers such as -0.005 -- is there anything obvious standing out at anyone? or am I using an incorrect function of some sort?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/
Share on other sites

if ($integer>=0.000)
{
$plusminus = '+';
$arrow = 'uparrow.png';
}
else
{
$plusminus = '-';
$arrow = 'downarrow.png';
}

 

The code above is not working correctly when dealing with numbers such as -0.005 -- is there anything obvious standing out at anyone? or am I using an incorrect function of some sort?

 

Thanks!

 

so $integer = -0.005 and it's not evaluating to false?

It concerns me that you have a decimal in a variable you're calling integer.

 

If at some point $integer is actually cast as an integer before this check, then -0.005 will become 0, in which case 0 is >= 0.000.

 

If this is not the case, then something else is going on.

It concerns me that you have a decimal in a variable you're calling integer.

 

If at some point $integer is actually cast as an integer before this check, then -0.005 will become 0, in which case 0 is >= 0.000.

 

If this is not the case, then something else is going on.

 

;)

E. Not enough information

It concerns me that you have a decimal in a variable you're calling integer.

 

If at some point $integer is actually cast as an integer before this check, then -0.005 will become 0, in which case 0 is >= 0.000.

 

If this is not the case, then something else is going on.

 

the $integer variable is being directly drawn from a .csv, so it varies depending on when it is parsed. And to the above poster, it does not return negative when the variable is -0.005, but it should.

 

below is the entire code (up until the echoing)

 

$file_handle = fopen("http://urlhere.com", "r");
while (!feof($file_handle) ) {
$integer = $line_of_text[4];
if ($integer>=0.000)
{
$plusminus = '+';
$arrow = 'uparrow.png';
}
else
{
$plusminus = '-';
$arrow = 'downarrow.png';
}

$line_of_text = fgetcsv($file_handle, 1024);

It concerns me that you have a decimal in a variable you're calling integer.

 

If at some point $integer is actually cast as an integer before this check, then -0.005 will become 0, in which case 0 is >= 0.000.

 

If this is not the case, then something else is going on.

 

the $integer variable is being directly drawn from a .csv, so it varies depending on when it is parsed. And to the above poster, it does not return negative when the variable is -0.005, but it should.

 

below is the entire code (up until the echoing)

 

$file_handle = fopen("http://urlhere.com", "r");
while (!feof($file_handle) ) {
$integer = $line_of_text[4];
if ($integer>=0.000)
{
$plusminus = '+';
$arrow = 'uparrow.png';
}
else
{
$plusminus = '-';
$arrow = 'downarrow.png';
}

$line_of_text = fgetcsv($file_handle, 1024);

 

Being used to other languages I would assume that line isn't logically correct where you are reading the character array into a variable and then checking it against a decimal. Since i'm not as familiar with the structure of php i'm going to assume you can do that with the correct output. Have you tried doing a simple echo $integer; before you hit that if statement, right after it's declared so that you can see what exactly is going into that if statement? Then perhaps you can eliminate a garbage in = garbage out problem

You might try echoing $integer, and then echoing (double)$integer ($integer cast as a double). See if you're getting what you expect to get.

 

(Just to clarify, do this right after $integer gets assigned...

 

$integer = $line_of_text[4];
echo gettype($integer).': '.$integer;
echo ((double)$integer);

)

You might try echoing $integer, and then echoing (double)$integer ($integer cast as a double). See if you're getting what you expect to get.

 

(Just to clarify, do this right after $integer gets assigned...

 

$integer = $line_of_text[4];
echo gettype($integer).': '.$integer;
echo ((double)$integer);

)

 

tried this, it echos correctly and doubles correctly

You might try echoing $integer, and then echoing (double)$integer ($integer cast as a double). See if you're getting what you expect to get.

 

(Just to clarify, do this right after $integer gets assigned...

 

$integer = $line_of_text[4];
echo gettype($integer).': '.$integer;
echo ((double)$integer);

)

 

tried this, it echos correctly and doubles correctly

 

Have you tried removing $integer in the if statement and replacing it with the actual value in question. That will determine if its the variable causing the problem or the logic.

You might try echoing $integer, and then echoing (double)$integer ($integer cast as a double). See if you're getting what you expect to get.

 

(Just to clarify, do this right after $integer gets assigned...

 

$integer = $line_of_text[4];
echo gettype($integer).': '.$integer;
echo ((double)$integer);

)

 

tried this, it echos correctly and doubles correctly

 

Have you tried removing $integer in the if statement and replacing it with the actual value in question. That will determine if its the variable causing the problem or the logic.

 

the actual value is $line_of_text[4] from the .csv -- I didn't have the $integer variable at all at first but was told this might fix the problem...

You might try echoing $integer, and then echoing (double)$integer ($integer cast as a double). See if you're getting what you expect to get.

 

(Just to clarify, do this right after $integer gets assigned...

 

$integer = $line_of_text[4];
echo gettype($integer).': '.$integer;
echo ((double)$integer);

)

 

tried this, it echos correctly and doubles correctly

 

Have you tried removing $integer in the if statement and replacing it with the actual value in question. That will determine if its the variable causing the problem or the logic.

 

the actual value is $line_of_text[4] from the .csv -- I didn't have the $integer variable at all at first but was told this might fix the problem...

 

What I mean is have you tried simply typing this

 

if((-0.005) >= 0.000) {
  bla bla
}

You might try echoing $integer, and then echoing (double)$integer ($integer cast as a double). See if you're getting what you expect to get.

 

(Just to clarify, do this right after $integer gets assigned...

 

$integer = $line_of_text[4];
echo gettype($integer).': '.$integer;
echo ((double)$integer);

)

 

tried this, it echos correctly and doubles correctly

 

Have you tried removing $integer in the if statement and replacing it with the actual value in question. That will determine if its the variable causing the problem or the logic.

 

the actual value is $line_of_text[4] from the .csv -- I didn't have the $integer variable at all at first but was told this might fix the problem...

 

What I mean is have you tried simply typing this

 

if((-0.005) >= 0.000) {
  bla bla
}

 

ohh -- no I haven't, and I thought of a similar thing after my last post:

 

does php, by default, know that the number is negative? or does it just see it as a dash and then the number? maybe I have to convert it to a currency or something similar...

That's why he recommended converting it to a double. Try the double casting in the if statement and see if that works.

 

tried it, and the output was 0 (when it should be 0.025 methinks?)... so this means that php is rounding the number up?

That's why he recommended converting it to a double. Try the double casting in the if statement and see if that works.

 

tried it, and the output was 0 (when it should be 0.025 methinks?)... so this means that php is rounding the number up?

 

Now i'm confused. Where are you getting this output from? Is there extra code involved in this that is doing a mathematical calculation? If so then it's needed at this point. If no then I don't understand what you are trying to do because none of the above mentioned code should be doing anything that calculates.

There HAS to be something wrong with the variable $integer. I did a quick testing using your IF conditional above and all of the following values produced a negative result, as expected:

-0.005 - float

'-0.005' - string

(string)' -0.005 ' - string with spaces and tab character

 

Also, not that it makes a difference, but why are you using 0.000 for the conditional test? Just use "0"

below is the entire code (up until the echoing)

 

$file_handle = fopen("http://urlhere.com", "r");
while (!feof($file_handle) ) {
$integer = $line_of_text[4];
if ($integer>=0.000)
{
$plusminus = '+';
$arrow = 'uparrow.png';
}
else
{
$plusminus = '-';
$arrow = 'downarrow.png';
}

$line_of_text = fgetcsv($file_handle, 1024);

 

WIAT A MINUTE! You are testing the value of $integer and integer is set using

$integer = $line_of_text[4];

 

But you don't set $line_of_text until AFTER you test $integer. So, $integer would have a null value when the IF condition is run. And, it just so happens that the IF condition will return true with a null value.

 

Change to this:

$file_handle = fopen("http://urlhere.com", "r");
while (!feof($file_handle) ) {
    $line_of_text = fgetcsv($file_handle, 1024);
    $integer = $line_of_text[4];
    if ($integer>=0.000)
    {
        $plusminus = '+';
        $arrow = 'uparrow.png';
    }
    else
    {
        $plusminus = '-';
        $arrow = 'downarrow.png';
    }
}

 

below is the entire code (up until the echoing)

 

$file_handle = fopen("http://urlhere.com", "r");
while (!feof($file_handle) ) {
$integer = $line_of_text[4];
if ($integer>=0.000)
{
$plusminus = '+';
$arrow = 'uparrow.png';
}
else
{
$plusminus = '-';
$arrow = 'downarrow.png';
}

$line_of_text = fgetcsv($file_handle, 1024);

 

WIAT A MINUTE! You are testing the value of $integer and integer is set using

$integer = $line_of_text[4];

 

But you don't set $line_of_text until AFTER you test $integer. So, $integer would have a null value when the IF condition is run. And, it just so happens that the IF condition will return true with a null value.

 

Change to this:

$file_handle = fopen("http://urlhere.com", "r");
while (!feof($file_handle) ) {
    $line_of_text = fgetcsv($file_handle, 1024);
    $integer = $line_of_text[4];
    if ($integer>=0.000)
    {
        $plusminus = '+';
        $arrow = 'uparrow.png';
    }
    else
    {
        $plusminus = '-';
        $arrow = 'downarrow.png';
    }
}

 

if there was a bowdown emoticon, you would get about 9 of them. THANK you for pointing out such a stupid error. of course the value right now is 0 so i can't check that it is working correctly, but I'm sure that was the problem. thank you muchly!

Ok now this old man is confused.

 

here is down and dirty test I just did...

$firstnumber = (-.025);
$secondnumber = 3.24;

echo $firstnumber;
echo "<br>";
echo $secondnumber;
echo "<br>";
if($firstnumber <0.00) {
echo "Negative";
}
echo "<br>";
echo $firstnumber * $secondnumber;
echo "<br> y";
$integer = $firstnumber;

if ($integer>=0.000)
{
$plusminus = '+';
$arrow = 'uparrow.png';
}
else
{
$plusminus = '-';
$arrow = 'downarrow.png';
}
echo "<br>";
echo $plusminus;

 

 

 

the output is...

 

-0.025

3.24

Negative

-0.081

y

-

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.