Value greater than value issue
Value greater than value issue
am 07.12.2007 21:01:17 von Acrobatic
Hello,
I know this will be an easy fix--but as of now I'm banging my head
against the wall. I need a fresh perspective from the group to see
what my problem is:
This is a simple accounting application, and the code below is
checking to see if a user's withdraw request is greater than their
available balance:
$withdraw_request = $_REQUEST['withdraw_amount']; // 543.21
$withdraw_maximum = $user['available_balance']; // 543.21
if($withdraw_request > $withdraw_maximum) {
echo "Insufficient funds.";
} else {
echo "Processing...";
}
In my application, both values are equal (543.21), but
$withdraw_request > $widthdraw_maximum still evaluates to TRUE, and
thus shows "Insufficient funds." If the two values are whole numbers,
like "543", then they evaluate the way I expect them to.
I've tried everything I can think of, like
$withdraw_request = floatval($_REQUEST['withdraw_amount']);
$withdraw_maximum = floatval($user['available_balance']);
as well as doing an "isnumeric" check on both values (they both return
true), but still no luck. If $withdraw_request is less than (<) the
$withdraw_maximum--ie 543.20 < 543.21, the script works fine.
Also, if I hard code the values, the script works fine. Somewhere
between pulling the maximum from the database and getting the
$_REQUEST variable things are getting lost in translation.
Thanks for any advice
Re: Value greater than value issue
am 07.12.2007 21:14:09 von Good Man
Acrobatic wrote in news:7bc09fbf-5b53-4d5d-bfc7-
326fee3c2ba9@d21g2000prf.googlegroups.com:
> Also, if I hard code the values, the script works fine. Somewhere
> between pulling the maximum from the database and getting the
> $_REQUEST variable things are getting lost in translation.
>
> Thanks for any advice
>
my quick suggestion would be to make sure your values really are what you
think they are... echo out to the screen
also, note that "floatval" is a function whereas "float" will evaluate a
string numerically.
so...
$withdraw_request = (float)$_REQUEST['withdraw_amount'];
$withdraw_maximum = (float)$user['available_balance'];
echo "comparing $withdraw_request and $withdraw_maximum
";
if($withdraw_request > $withdraw_maximum) {
echo "Insufficient funds.";
} else {
echo "Processing...";
}
?>
Re: Value greater than value issue
am 07.12.2007 21:52:31 von Jerry Stuckle
Acrobatic wrote:
> Hello,
>
> I know this will be an easy fix--but as of now I'm banging my head
> against the wall. I need a fresh perspective from the group to see
> what my problem is:
>
> This is a simple accounting application, and the code below is
> checking to see if a user's withdraw request is greater than their
> available balance:
>
>
> $withdraw_request = $_REQUEST['withdraw_amount']; // 543.21
> $withdraw_maximum = $user['available_balance']; // 543.21
>
> if($withdraw_request > $withdraw_maximum) {
> echo "Insufficient funds.";
> } else {
> echo "Processing...";
> }
>
>
> In my application, both values are equal (543.21), but
> $withdraw_request > $widthdraw_maximum still evaluates to TRUE, and
> thus shows "Insufficient funds." If the two values are whole numbers,
> like "543", then they evaluate the way I expect them to.
>
> I've tried everything I can think of, like
>
> $withdraw_request = floatval($_REQUEST['withdraw_amount']);
> $withdraw_maximum = floatval($user['available_balance']);
>
> as well as doing an "isnumeric" check on both values (they both return
> true), but still no luck. If $withdraw_request is less than (<) the
> $withdraw_maximum--ie 543.20 < 543.21, the script works fine.
>
> Also, if I hard code the values, the script works fine. Somewhere
> between pulling the maximum from the database and getting the
> $_REQUEST variable things are getting lost in translation.
>
> Thanks for any advice
>
Welcome to the world of floating point numbers - where values are not
generally exact.
See http://www.php.net/manual/en/language.types.float.php for a
discussion on it.
For this particular instance, you should be able to solve the problem with:
$val1=(intval)((floatval($_REQUEST['withdraw_amount']) + 0.005)* 100);
This gets the float value, adds 0.005 to it then multiplies by 100. It
then gets the integer value of the result. Repeat for the available
balance.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Value greater than value issue
am 07.12.2007 22:44:41 von Acrobatic
> For this particular instance, you should be able to solve the problem with:
>
> $val1=(intval)((floatval($_REQUEST['withdraw_amount']) + 0.005)* 100);
>
> This gets the float value, adds 0.005 to it then multiplies by 100. It
> then gets the integer value of the result. Repeat for the available
> balance.
Thanks guys for the advice. Jerry, your workaround did the trick.