Been a long time since ive really touched Javascript, and i've forgotten much of the syntax. That said, I can have a complex piece of code in front of me and explain how it works, but cannot write for peanuts.

I'm basically helping a friend, who had two text fields, which should take numeric values only. This will be the only error checking required. All we need to do is, when the form is processed, multiply the two values entered in each text field, and display the result in an alert box. I also need to get this figure, add 25% to it, and have it multiplied by a figure entered in a third text box, which will also display in the alert box, in the same sentence.

Only this is, it looks a little complicated for the outcome, the code above the main function is *largely* greek to me, and i'll definetly have a hard time getting my bud to understand it! Though I gather I can prune it somehow.

martin_narg

08-10-2005, 03:53 PM

the bit above the Number.prototype.toDecimals is an extension to the standard javascript Number object. The toDecimals method converts the number to the specified number (or two if not specified) of decimal places (with appropriate zeros).

var step1 = this*Math.pow(10, d); // multiply number (referred to as this) by 10 to the power of the number of decimal places required
var step2 = Math.round(step1); // round that result
var step3 = step2 / Math.pow(10, d) // divide the rounded number by 10 to the power of the number of decimal places required

return step3; // return the rounded number
}

There's plenty of tutorials on the web explaining the maths behind rounding numbers in greater detail, I just hope i've given you some initial insight into it.