473,800 Members | 3,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_reque st = $_REQUEST['withdraw_amoun t']; // 543.21
$withdraw_maxim um = $user['available_bala nce']; // 543.21

if($withdraw_re quest $withdraw_maxim um) {
echo "Insufficie nt funds.";
} else {
echo "Processing..." ;
}
In my application, both values are equal (543.21), but
$withdraw_reque st $widthdraw_maxi mum still evaluates to TRUE, and
thus shows "Insufficie nt 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_reque st = floatval($_REQU EST['withdraw_amoun t']);
$withdraw_maxim um = floatval($user['available_bala nce']);

as well as doing an "isnumeric" check on both values (they both return
true), but still no luck. If $withdraw_reque st is less than (<) the
$withdraw_maxim um--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 2516
Acrobatic <jb****@gmail.c omwrote in news:7bc09fbf-5b53-4d5d-bfc7-
32**********@d2 1g2000prf.googl egroups.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_reque st = (float)$_REQUES T['withdraw_amoun t'];
$withdraw_maxim um = (float)$user['available_bala nce'];

echo "comparing $withdraw_reque st and $withdraw_maxim um <br>";

if($withdraw_re quest $withdraw_maxim um) {
echo "Insufficie nt 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_reque st = $_REQUEST['withdraw_amoun t']; // 543.21
$withdraw_maxim um = $user['available_bala nce']; // 543.21

if($withdraw_re quest $withdraw_maxim um) {
echo "Insufficie nt funds.";
} else {
echo "Processing..." ;
}
In my application, both values are equal (543.21), but
$withdraw_reque st $widthdraw_maxi mum still evaluates to TRUE, and
thus shows "Insufficie nt 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_reque st = floatval($_REQU EST['withdraw_amoun t']);
$withdraw_maxim um = floatval($user['available_bala nce']);

as well as doing an "isnumeric" check on both values (they both return
true), but still no luck. If $withdraw_reque st is less than (<) the
$withdraw_maxim um--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($_REQ UEST['withdraw_amoun t']) + 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*******@attgl obal.net
=============== ===

Dec 7 '07 #3
For this particular instance, you should be able to solve the problem with:
>
$val1=(intval)( (floatval($_REQ UEST['withdraw_amoun t']) + 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...@t obyinkster.co.u k>
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...@t obyinkster.co.u k>
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*******@attgl obal.net
=============== ===

Dec 10 '07 #7

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

Similar topics

9
1966
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 he only enter values into the first four. I need to ALWAYS extract the final or last field that has a value. In the example below this would be PriceField5 which has a value of $162,000. The user stopped at this field and left the rest blank.
2
1844
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 l1=256; long l2=250; wchar_t c1=(wchar_t)l1; wchar_t c2=(wchar_t)l2; s+=l1;
3
1528
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 problem is, that each value itself can contain comma as well, in which case i have no way to separate my values. For example, my list box contains Smith, John and Cohen, David, two values. When the selection box is posted, i am getting "Smith, John,...
2
1342
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 zero. i have looked into the other messages which guide to override the paint procedure of the datagrid, make a new class etc. i am not able to understand the exact way of doing the mentioned
16
2150
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 onClick, change the selected option of a Select via the Value of the select only? I can't use the 'selectedIndex' because it doesn't relate to the value of the option. Thanks!
5
2761
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 select box with three options and a separate hidden field. The value of a hidden field is dependent upon which of the three options is selected. For example, the three select options are 1, 2, 3... if 1 is selected, the value of the hidden field is...
0
4276
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. That part works. However, my problem is that the form lets the user choose from input files; the labels are updated, the textboxes are not. This seems to me like it is a ViewState issue, I just can't figure out how to fix that... Apparently the...
13
3833
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 have an Asp.Net app that's in 2.0 framework. I have a page that is sort of a storefront sort of page that has a product, a quantity and a button for "Add To Cart" and one for "Add To Wishlist". I'd like to disable those two buttons if the...
1
6077
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" value="<<<<need to insert here(optvalue)>>>>"> when i try it works only for the selected value and in an instance it clears the <input...>.
0
9553
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10509
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10256
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10039
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9095
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7584
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4152
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.