PDA

View Full Version : Help - Math.round object and disappearing zeros


MattyUK
07-13-2002, 06:23 PM
Hi

wonder if somebody could help me out with this one.

I'm using the following code:

function cpu()
{
var cqu = "cof.cqr.value";
var cpu = "cof.cphr.value";
var amount=Math.round(((eval(cqu))*(eval(cpu)))*100)/100;
var final = "cof.cpr.value="+amount;
eval(final);
}

Note:cof.cqr, cof.cphr & cof.cpr =the dom path to form elements.

This takes a quantity value from one text box and * another value with it returning the result and rounding down to two decimal places.

Problem is that trailing zeros get lost along the way. I need to display them. So 2*1.25 = 2.5 at them moment but I need it to read 2.50. The solution shouldn't upset other value by just adding a 0 on the end whilly nilly. For example 2*1.24=2.48 not 2.480

I kinda need an answer on this one urgently but is has baffled :confused: me. Can anyone help please?

MattyUK

joh6nn
07-13-2002, 06:26 PM
i think i know how to help you, but i need to see the rest of your page, i think.

MattyUK
07-13-2002, 06:42 PM
Thanks that would be great.

http://www.tubscorner.co.uk/codingforums.htm

You'll have to enter the demo pruice int eh box then go and edit the quantity amount/ the function is called onblur of the quantity text field.

Thanks for your input.

MattyUK

jkd
07-13-2002, 06:47 PM
IE5.5+ and NS6+ both support a toFixed() method of a number object, which returns a string of the number with the decimals to X amount of fixed digits:

12.325794.toFixed(2) == "12.33"

:)

MattyUK
07-13-2002, 06:51 PM
That is so cool.

Thank you very much. I had no idea that was around.

It'll do me fine.

Just out of curiosity any idea how it was achieved with older browsers?

Either way thank you again. You've made my day!:thumbsup: :):D

MattyUK

nolachrymose
07-13-2002, 07:46 PM
Math.round(4.18239*100)/100 == 4.18

Hope that helps!

Happy coding! :)

MattyUK
07-13-2002, 07:49 PM
Cheers nolachrymose

I'd already got that far. but that method kills trailing zeros. thankfully the toFixed(2) bit can put them back.

MattyUK

BrainJar
07-13-2002, 07:58 PM
This function will do the equivalent of toFixed() for browsers that don't support that method:


function formatNumber(n, p) {

var d, i, m, t;

// check arguments
if (isNaN(n) || isNaN(p) || p != Math.round(p) || p < 0)
return "Error";

// if zero decimal places was specified, just round the number
if (p == 0)
return Math.round(n).toString();

// otherwise, round to the given number of decimal places
m = Math.pow(10, p);
t = Math.round(n * m) / m;
t = t.toString();

// determine how many zeros need to be added after the decimal point
if (t.indexOf(".") < 0)
t += ".";
d = t.length - 1 - t.indexOf(".");

// add the trailing zeros
for (i = 0; i < p - d; i++)
t += "0";

return(t);
}

MattyUK
07-14-2002, 08:16 AM
Thanks all.

As a new member I'm really impressed with the forum.

Thanks again for all your help.

MattyUK

MattyUK
07-14-2002, 08:19 AM
Administrator:

I guess this thread can be closed now. Happily got my answers.

Should I just leave it?

I'll do that and let you pick it up.

Cheers. :)