zerGinfested Posted February 23, 2010 Share Posted February 23, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/ Share on other sites More sharing options...
aeroswat Posted February 23, 2010 Share Posted February 23, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1016975 Share on other sites More sharing options...
ialsoagree Posted February 23, 2010 Share Posted February 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1016980 Share on other sites More sharing options...
aeroswat Posted February 23, 2010 Share Posted February 23, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1016982 Share on other sites More sharing options...
zerGinfested Posted February 23, 2010 Author Share Posted February 23, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1016984 Share on other sites More sharing options...
aeroswat Posted February 23, 2010 Share Posted February 23, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1016991 Share on other sites More sharing options...
ialsoagree Posted February 23, 2010 Share Posted February 23, 2010 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); ) Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017005 Share on other sites More sharing options...
zerGinfested Posted February 23, 2010 Author Share Posted February 23, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017106 Share on other sites More sharing options...
aeroswat Posted February 24, 2010 Share Posted February 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017410 Share on other sites More sharing options...
zerGinfested Posted February 24, 2010 Author Share Posted February 24, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017523 Share on other sites More sharing options...
aeroswat Posted February 24, 2010 Share Posted February 24, 2010 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 } Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017531 Share on other sites More sharing options...
zerGinfested Posted February 24, 2010 Author Share Posted February 24, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017539 Share on other sites More sharing options...
aeroswat Posted February 24, 2010 Share Posted February 24, 2010 That's why he recommended converting it to a double. Try the double casting in the if statement and see if that works. Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017542 Share on other sites More sharing options...
zerGinfested Posted February 24, 2010 Author Share Posted February 24, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017554 Share on other sites More sharing options...
aeroswat Posted February 24, 2010 Share Posted February 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017559 Share on other sites More sharing options...
Psycho Posted February 24, 2010 Share Posted February 24, 2010 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" Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017563 Share on other sites More sharing options...
Psycho Posted February 24, 2010 Share Posted February 24, 2010 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'; } } Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017567 Share on other sites More sharing options...
zerGinfested Posted February 24, 2010 Author Share Posted February 24, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017571 Share on other sites More sharing options...
litebearer Posted February 24, 2010 Share Posted February 24, 2010 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 - Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017670 Share on other sites More sharing options...
KevinM1 Posted February 24, 2010 Share Posted February 24, 2010 That's the correct result. $integer = $firstnumber = -0.025, which is less than 0. Your conditional tests whether or not it's greater than or equal to 0, which it is not. Quote Link to comment https://forums.phpfreaks.com/topic/193112-if-statement-with-negative-decimals/#findComment-1017683 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.