Casting problem..help!

  • Thread starter The Natural Philosopher
  • Start date
T

The Natural Philosopher

I have this routine that is called when a salesperson over rides a
selling price.

It has two problems. One is that rounding errors occasionally meana it
rounds the value down a penny when it should not. That I can fix.

The second is that sometimes, for no discernible reason, it blows up
spectacularly and sets line totals that are monstrous and often negative.

I cannot reliably reproduce this, so I am appealing for someone to look
through this not very good code, and indicate where a possible problem
might arise.


function price_override(id)
{
document.getElementsByName('line_discount_'+id)[0].value='0';
document.getElementById('discount_'+id).innerHTML='0.00';
document.getElementsByName('discount')[0].value='0';

var oldtotal=Math.round((document.getElementById('total_'+id).innerHTML)
*100)/100;

var newtotal=Math.round((document.getElementById('qty_'+id).innerHTML *
document.getElementsByName('sale_price_'+id)[0].value)*100)/100;

document.getElementById('total_'+id).innerHTML=newtotal;

var grandtotal=
Math.round((document.getElementsByName('grand_total')[0].value)*100)/100;

grandtotal=grandtotal+(newtotal-oldtotal);
document.getElementsByName('grand_total')[0].value=grandtotal;
document.getElementsByName('order_total')[0].value+=(newtotal-oldtotal);

do_discount(); // this just does other irrelavant stuff and calculstes
order total magins and discounts.

}
 
G

Garrett Smith

The said:
I have this routine that is called when a salesperson over rides a
selling price.

It has two problems. One is that rounding errors occasionally meana it
rounds the value down a penny when it should not. That I can fix.
OK.

The second is that sometimes, for no discernible reason, it blows up
spectacularly and sets line totals that are monstrous and often negative.
[snip]
The code you posted is not a complete working example. A complete
example contains the bare minimum code required to reproduce the
problem.

Posted code should be formatted to 72 chars and indented with spaces.
http://jibbering.com/faq/#postCode

Please state the conditions that triggered the problem. When you observe
the problem, state what the values of the related fields are. When you
get the odd result, write down the values of the related fields. Then
try to reproduce it.

In your code, you probably want to check to make sure the value is in
range.

var xVal = +x.value;
if(xVal > 0 && xVal < 100) {
// math.
}

Otherwise, x.value might be "". The empty string, or whitespace has a
numeric value of 0; e.g. "" * 100 === 0;

This "whitespace" is defined in ECMA-262r3, but as we have seen with \s,
browsers deviate from that.

So, the Mongolian Vowel Separator, in Firefox 3.5. for example, is not.

The following should all be true:
// Mongolian Vowel Separator
0 === +"\u180E"; // should be true, false in FF3.5
// Space
0 === +"\u0020";
// No-break Space
0 === +"\u00a0";

At any rate, it is a good idea to check the field's value before doing
math on it.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>, Wed,
28 Oct 2009 14:16:13 said:
I have this routine that is called when a salesperson over rides a
selling price.

It has two problems. One is that rounding errors occasionally meana it
rounds the value down a penny when it should not. That I can fix.

The second is that sometimes, for no discernible reason, it blows up
spectacularly and sets line totals that are monstrous and often
negative.

Do the calculation in pence, rather than in pounds. There will then be
rounding errors in the additive/subtractional parts of the code, as long
as the maximum amount handled is under ninety trillion pounds.

For each input control, the value of which will be a string, convert to
Number on first reading and then do not refer to the control again in
that unit of calculation.

Cord = <control>.value
if (Invalid(Cord)) { <Report> ; <quit> }
Datum = +Cord

"Invalid" is usually best done with a RegExp; see
<URL:http://www.merlyn.demon.co.uk/js-valid.htm>.




<FAQENTRY> 5.0 How do I read an input control safely? As above.

<FAQENTRY> Normally, one reads Numbers, manipulates them, then outputs
them. Therefore, the present FAQ 5.x entries should be in the order 3 4
2 5 1 although 5.5 would be better in a Maths section.

<FAQENTRY> FAQ 9 Windows and Frames is too big. What goes in it should
be governed with what questioners will think, not on whether the window,
document. or body Objects are involved. Questions specific to Frames
can have their own major section. 9.8 does not belong, it needs to be
in a Miscellaneous section.

<FAQENTRY> 12.3 is not all that helpful; it offers no work-rounds. A
page can request, to the user, that a copy of itself be run locally : it
then has some access to the local file system. A page can request, to
the user, that a copy of some local system data be pasted into a
textarea or other control.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,082
Messages
2,570,589
Members
47,211
Latest member
Shamestone

Latest Threads

Top