473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Value greater than value issue

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
Dec 7 '07 #1
6 2497
Acrobatic <jb****@gmail.comwrote in news:7bc09fbf-5b53-4d5d-bfc7-
32**********@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...

<?php

$withdraw_request = (float)$_REQUEST['withdraw_amount'];
$withdraw_maximum = (float)$user['available_balance'];

echo "comparing $withdraw_request and $withdraw_maximum <br>";

if($withdraw_request $withdraw_maximum) {
echo "Insufficient funds.";
} else {
echo "Processing...";
}

?>
Dec 7 '07 #2
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.
js*******@attglobal.net
==================

Dec 7 '07 #3
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.
Dec 7 '07 #4
Acrobatic wrote:
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:
To build on Jerry's post, floats are a really bad idea for representing
currencies. They're just too woolly. As PHP doesn't include any fixed-
precision decimal data type, the best option is to represent money as an
integer -- using the smallest unit of currency possible (e.g. cents
rather than euros; pence rather than pounds).

You then reformat it as a price in the larger unit of currency only when
it needs to be displayed.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 21:23.]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
Dec 8 '07 #5
On Dec 8, 5:51 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
Acrobatic wrote:
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:

To build on Jerry's post, floats are a really bad idea for representing
currencies. They're just too woolly. As PHP doesn't include any fixed-
precision decimal data type, the best option is to represent money as an
integer -- using the smallest unit of currency possible (e.g. cents
rather than euros; pence rather than pounds).

You then reformat it as a price in the larger unit of currency only when
it needs to be displayed.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 21:23.]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
Geez, that must have been a doozey bug. :)
Dec 9 '07 #6
allain wrote:
On Dec 8, 5:51 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
>Acrobatic wrote:
>>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:
To build on Jerry's post, floats are a really bad idea for representing
currencies. They're just too woolly. As PHP doesn't include any fixed-
precision decimal data type, the best option is to represent money as an
integer -- using the smallest unit of currency possible (e.g. cents
rather than euros; pence rather than pounds).

You then reformat it as a price in the larger unit of currency only when
it needs to be displayed.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 21:23.]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/

Geez, that must have been a doozey bug. :)
Not at all. A very common bug for anyone who's worked with floating
point values for more than six months or so.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Dec 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jason | last post by:
Access 2000 I need some help interogatting a table and extracting via ASP the final field in a row which has a value. In other words, I have a maximum of 10 fields but, at the user level he may...
2
by: jackie | last post by:
Here is my prolem: i try to output wstring with unicode value greater than 255(for each wchar_t) to a file such as test.tmp. Here is my code: wofstream outdata("test.tmp"); wstring s; long...
3
by: Julia | last post by:
I have a multi-value drop down box (<select name="Users" multiple>...) When my form is posted, i can read Request.Form.Item("Users"), a string that contains all values separated by comma. The...
2
by: hemant | last post by:
hello everybody, I am having a datagrid which has data regarding customers. it has a penalty column, and i want to show the entire record of the customer in red whose penalty is greater than...
16
by: @sh | last post by:
Probably very simple, but I have a SELECT box The value of each option in the Select box will be a number, for instance... <option value="5">A test entry</option> Therefore, how do I, via an...
5
by: dvwool | last post by:
Hello, Another newbie here... I've been trying to make this work for days now and have finally decided to post as I can't seem to get it to work. Here's what I'm trying to do... I have a...
0
by: Zark3 | last post by:
Hi all, In a web form I use a for loop to dynamically add TextBox'es to a page (one for each item in an input file). I can get the input values and I can display them in Label's and TextBox'es....
13
by: HockeyFan | last post by:
I'm not sure how to exactly ask this, but expect that it's a Javascript solution. However, I'll ask the question and maybe someone can point me to some existing code that will do what I need. I...
1
by: bodilima | last post by:
Dear All, I'm new to this! I wanna insert selected values from form "SaveModel" to form "SaveMake" <input name="makeID" value="<<<<need to insert here(x)>>>>"> & <input name="make"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.