473,594 Members | 2,720 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scrip drops a penny when amount ends with .d0

I call a function that takes the unit price and quantity ordered to
create an amount...it looks something like this.

function calculateCost()
{
quantity=docume nt.RFO.quantity .value;
unitPrice=docum ent.RFO.unitPri ce.value;
total=0;
if(isPositiveIn teger(quantity) )
{
quantity=parseF loat(quantity)
{
total=quantity * unitPrice;
}
}
document.RFO.to tal.value=forma t(total)
}

if a person enters a unit price that ends with .10, .20, .30, ..., .90,
my code drops a penny off the total? However if the price ends with .1,
..2, .3, ..., .9, my code works fine.
Seems it has a problem calculating prices ending with 0 unless it's .00

May 10 '06 #1
35 2276
VK

ey****@ncsa.uiu c.edu wrote:
if a person enters a unit price that ends with .10, .20, .30, ..., .90,
my code drops a penny off the total? However if the price ends with .1,
.2, .3, ..., .9, my code works fine.
Seems it has a problem calculating prices ending with 0 unless it's .00


<http://www.jibbering.c om/faq/#FAQ4_6>

May 10 '06 #2
Why would I need to convert a number into a string?

May 10 '06 #3
ey****@ncsa.uiu c.edu wrote:
Why would I need to convert a number into a string?


Why do you want to know?

Quote, please!

May 10 '06 #4
You told me to go to http://www.jibbering.com/faq/#FAQ4_6
this was the answer to my question....

http://www.jibbering.com/faq/#FAQ4_6 explains how to convert a number
into a string

May 10 '06 #5
VK

ey****@ncsa.uiu c.edu wrote:
Why would I need to convert a number into a string?


Because you don't want to have your penni lost. What was a question,
was it? <11************ **********@e56g 2000cwe.googleg roups.com>

May 10 '06 #6
ey****@ncsa.uiu c.edu wrote:
I call a function that takes the unit price and
quantity ordered to create an amount...it looks
something like this.

function calculateCost()
{
quantity=docume nt.RFO.quantity .value;
unitPrice=docum ent.RFO.unitPri ce.value;
total=0;
The variables - quantity -, - unitPrice - and - total - should be
declared local to the function using the - var - keyword whenever there
is no good reason for them being global (and there doesn't appear to be
any reason for that).
if(isPositiveIn teger(quantity) )
{
quantity=parseF loat(quantity)
Why - parseFloat -? Surly a quantity has got to be an integer (and
particularly if - isPositiveInteg er - does what its name suggests), so
why not - parseInt - and if you are verifying that the string contains a
representation of a positive integer why not let the type conversion
implicit in multiplication do that job.
{ ^ total=quantity * unitPrice;
} ^

What are these opening and closing braces doing here? They form a Block
statement, but there is no reason for a Block statement appearing here.
}
document.RFO.to tal.value=forma t(total)
}

if a person enters a unit price that ends with .10,
.20, .30, ..., .90, my code drops a penny off the
total? However if the price ends with .1, .2, .3,
..., .9, my code works fine. Seems it has a problem
calculating prices ending with 0 unless it's
.00


There is nothing in this code that would suggest the phenomenon you
describe. A quick test of the javascript operations you are using finds
no evidence of differing results using strings with trailing zeros and
the same strings with the trailing zeros stripped off:-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
</head>
<body>
<pre>
<script type="text/javascript">

var l = [
'1.00','1.10',' 1.20','1.30','1 .40','1.50','1. 60','1.70','1.8 0','1.90',
'2.00','2.10',' 2.20','2.30','2 .40','2.50','2. 60','2.70','2.8 0','2.90',
'3.00','3.10',' 3.20','3.30','3 .40','3.50','3. 60','3.70','3.8 0','3.90',
'4.00','4.10',' 4.20','4.30','4 .40','4.50','4. 60','4.70','4.8 0','4.90',
'5.00','5.10',' 5.20','5.30','5 .40','5.50','5. 60','5.70','5.8 0','5.90',
'6.00','6.10',' 6.20','6.30','6 .40','6.50','6. 60','6.70','6.8 0','6.90',
'7.00','7.10',' 7.20','7.30','7 .40','7.50','7. 60','7.70','7.8 0','7.90',
'8.00','8.10',' 8.20','8.30','8 .40','8.50','8. 60','8.70','8.8 0','8.90',
'9.00','9.10',' 9.20','9.30','9 .40','9.50','9. 60','9.70','9.8 0','9.90'
];
var q = [
'1','2','3','4' ,'5','6','7','8 ','9'
];

function isPositiveInteg er(){
return true;
}

function calculateCost2( x, y){
quantity = q[y];
unitPrice = l[x];
total=0;
var unitPrice2, total2;
if(isPositiveIn teger(quantity) ){
quantity = parseFloat(quan tity)
{
total = quantity * unitPrice;
unitPrice2 = unitPrice.subst ring(0, (unitPrice.leng th - 1));
total2 = quantity * unitPrice2;
}
}
return (quantity+' * '+unitPrice+' = '+total+'\n'+qu antity+' * '+
unitPrice2+' = '+total2+'\t\t' +
((total != total2)?'****** **** Differ **********':'') );
}

for(var c = 0;c < q.length;++c){
for(var d = 0;d < l.length;++d){
document.write( calculateCost2( d, c)+'\n\n')
}
}

</script>
</per>
</body>
</html>

With no evidence of the phenomena it cannot be attributed. You will have
to create an example that actually shows this in action. It would be a
good idea to mention the browser you are using in case this is an
implementation specific bug (I used IE and Mozilla for testing the above
code).

(Incidentally, although the FAQ reference VK posted is relevant to the
issues of money calculations and number to formatted string
transformations , it cannot account for the symptoms you describe. VK
should generally be ignored, he does not know javascript at all and as a
result he cannot see what would be expected from executing javascript
code, and so is incapable of attributing effects to causes except by
(inevitably often wrong) guess-work.)

Richard.
May 10 '06 #7
JRS: In article <11************ **********@e56g 2000cwe.googleg roups.com>
, dated Tue, 9 May 2006 08:32:06 remote, seen in
news:comp.lang. javascript, ey****@ncsa.uiu c.edu posted :
I call a function that takes the unit price and quantity ordered to
create an amount...it looks something like this.

function calculateCost()
{
quantity=docume nt.RFO.quantity .value;
unitPrice=docum ent.RFO.unitPri ce.value;
total=0;
if(isPositiveIn teger(quantity) )
{
quantity=parseF loat(quantity)
{
total=quantity * unitPrice;
}
}
document.RFO.to tal.value=forma t(total)
}

if a person enters a unit price that ends with .10, .20, .30, ..., .90,
my code drops a penny off the total? However if the price ends with .1,
.2, .3, ..., .9, my code works fine.
Seems it has a problem calculating prices ending with 0 unless it's .00


Your code lacks isPositiveInteg er and format, so cannot be tested by me.

Your first move should be to omit use of format, since you need to know
whether there is an error up to that stage; or to try alert(total)
before that. H'mmm - I notice you use total in two ways - that is
at best injudicious; change one of them, to be safe.

It seems illogical to use parseFloat on something just shown to be
integer; you could use unary + instead; you don't need that line, unless
you must allow trailing characters.

Get a proper newsreader, with spelling-checker; Google is a bad
interface; read newsgroup FAQ 2.3, 4.6, 4.7 before posting a reply.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
May 10 '06 #8
I wrote a basic code because the real code has so much other stuff
going on. My code allows the user to add as many lines of items to be
purchased...so I have no idea how many items to total up for line item
or main total, so this statement gets all the sets of values.
The problem is only seen when 3 or 6 items are purchased at a cost of
..70 cents...that's the only time it's wrong.
My code gives the following totals
3 * .70 = 2.09
6 * .70 = 4.19

btw, I'm not asking for anyone's thoughts using eval...I had no choice
but to use it, the information is coming from some visual basic
statements and would not work without the eval...I also had to use the
Visual Basic to create collapsible / expandable items to be purchased
area.

My code:
for(i=2;i<=nrow ;i++) {
eval("quant" + i + "=document.RFO. quant_" + i + ".value");
eval("unit" + i + "=document.RFO. unit_" + i + ".value");
eval("tot" + i + "=0");

eval("tquant=qu ant" + i + "");
eval("tunit=uni t" + i + "");
eval("ttot=tot" + i + "");

if(isPositiveIn teger(tquant))
{
tquant=parseFlo at(tquant)
{
ttot=tquant * tunit;
}
}
eval("document. RFO.tot_" + i + ".value=format( ttot)");
}
For testing, I have stripped to code down to nothing which only ran on
the first set of values...the code I listed before...and I get the same
results. I then changed it a bit after reading your statments
here...even removing the isPositiveInteg er statment to make sure the
problem was not there. I still get the same result, every combination
of values work except 3 or 6 items at a price of ".70" or ".7"

function calculateCost()
{
nrow=document.R FO.nrow.value;
quant1=document .RFO.quant_1.va lue;
unit1=document. RFO.unit_1.valu e;
tot1=0;
quant1=parseInt (quant1)
unit1=parseFloa t(unit1)
{
tot1=quant1 * unit1;
}
}
document.RFO.to t_1.value=forma t(tot1)

May 10 '06 #9
if(isPositiveIn teger(tquant))
{
document.RFO.el ements["tot_"+i].value=tquant*t unit;
}

You haven't shown us your format() function. As suggested
before, run without it to determine if it's the problem.


keep reading...I removed the function as well as other stuff to test
the code.

May 10 '06 #10

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

Similar topics

15
4293
by: Sven Templin | last post by:
Hello all, our configuration is as following described: - OS: Windows 2000 - Apache server 1.3 - Php 3.8 - MS outlook client 2000 _and_ no SMTP server available in the whole intranet.
77
5663
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a database from SQL Server to Oracle or DB2 or vice versa... and that's it.... And a lot of these enterprises don't need it as they already know what database they are going to use and they don't plan on switching in and out database in the first...
65
4040
by: Pmb | last post by:
I'm confused as to what the compiler error message I'm getting is refering to. Can someone take a gander and let me know what I did wrong? The program is below. When I compile it I get the following error ______________________________ Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland d:\temp\complex\temp.cpp: Error E2333 d:\temp\complex\temp.cpp 73: Class member 'Complex::conjugate(Complex)' declared outside its class Error...
7
4618
by: aagarcia | last post by:
My sum from my a report footer total at the end of a report is off by a penny! The data totals coming from query are correct. Only the calculations (=Sum()doesn't add the penny. I've tried changing the format and changing the decimal places but its not working. hourly wage is 39.59. employee hour total: 316.72, 108.87, 277.13, 138.57, 138.57 total should be 979.86 but the total from sum total says $979.85
4
2838
by: Matt Billock | last post by:
Hello everyone, I am having some issues with what should be an insanely simple problem. Basically, I'm working on an implementation of the TSP algorithm (I have a static set of 5 locations, so I'm just brute forcing my way through it), and during execution I build a list of 120 potential distances to be travelled, stored in an array of type double. I then have another function which traverses this array and locates the position of the...
1
2338
by: Sean Wolfe | last post by:
I have this wierd bizarrre problem that I'm experienceing with the Response.Redirect() method. I have a site where the users are on a particular page in SSL. When they post the data back, and the processing is done, it then tries to redirect them back to the non-SSL site. But the site is on a non-starndard port number. I place in the Response.Redirect() method the Fully qualified path to the URL and when ASP.NET makes the Response, the...
4
4497
by: Joe-Paul | last post by:
Hi: I'm running a simple query on an Access Table from VB6.0. The operator can make several different selections. Based on their selection, a different, specific SQL needs to be run. So, when the operator says... "Do it"...the click procedure goes to a Function (based on their selection)...the specific function builds the correct SQL and returns it...then it sends that query to the final procedure to run the sql, retrieve the data and...
1
1084
by: mahsina | last post by:
I have the following scrip that works in XP SP2 but not in XP SP1 // Script sub UpLoad (UploadFileName) dim b1 b1=&H4000000 ' is required set objStream =CreateObject("ADODB.Stream") set objRecord =CreateObject("ADODB.Record") set cnn =CreateObject("ADODB.Connection") cnn.Open "provider=MSDAIPP.DSO;data
1
1982
by: SANDY1722 | last post by:
Hi, I have a shell script, test.ksh which should read the input from another file, say 1.txt test.ksh: #!/usr/bin/ksh datasource=$1
0
7946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7876
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
8251
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...
0
8372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6654
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
5739
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
5408
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3897
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1478
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.