Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 Decimal places!!!! 2

Status
Not open for further replies.

TrueJoker

Technical User
Jun 14, 2006
115
GB
I ahve set up some calculations using javascript code, but to my horror it displays way too many decimal places, how can i resrict the calculations result to 2 decimal places?

This is the snippet of code i am using!

Code:
function dm(amount) 
{
  string = "" + amount;
  dec = string.length - string.indexOf('.');
  if (string.indexOf('.') == -1)
  return string + '.00';
  if (dec == 1)
  return string + '00';
  if (dec == 2)
  return string + '0';
  if (dec > 3)
  return string.substring(0,string.length-dec+3);
  return string;
}
Code:
incvat = vat + subtotal;
	document.form1.incvat.value = dm(eval(incvat));

The calculation is made up of a subtotal with the VAT being added onto it. Some results do only display 2 decimal places but more often than not it displays recuring decimal places!

Thanx in advance for any help!
 
TrueJoker, use this:

Code:
incvat.toFixed(2);

Cheers

Nick
 
sorry to be a noob but where would i implement it?
 
Another way you for working with decimals is to use the toPrecision method. Here's an example of both for future ref:

Code:
var MyNum = 501.2347;
var result;

//Using toFixed
result = MyNum.toFixed(2); //result 501.23

//Using toPrecision
result = MyNum.toPrecision(4); // result 501.2

Nick
 
TJ, I'm assuming your function dm is just to handle the decimal. Since you no longer need this then just put it here:

Code:
incvat = vat + subtotal;
document.form1.incvat.value = incvat.toFixed(2);

Nick
 
it doesnt ahve any affect on the calculations when i try it! Do i have to specify the toFixed option as anything for it to work?
 
TJ, it should work just fine. Here's an example but can you also post your most up to date code?

Code:
var vat = 91.565;
var subtotal = 523.23;
var incvat;

incvat = eval(vat + subtotal);

document.write (incvat.toFixed(2));

Rememebr JS is case sensitive so if you have tofixed for example, it will not work.

Nick
 
I changed the calculation like you showed me before

Code:
incvat = vat + subtotal;
	document.form1.incvat.value = incvat.toFixed(2);

But there was no reaction, the calculation still worked but it still returned a recuring answer. I was just curious if i had to assign or define toFixed as anything?
 
I have managed to find a piece of code which seems to work, i was reading through some tutorials and foudn ti in one of them!

Code:
incvat = Math.round((vat + subtotal) * 100)/100;
	document.form1.incvat.value = dm(eval(incvat));

The only issue is that if the result is £10.50 or any decimal with a 0 on the end etc then it displays it as £10.5 but i can live with it lol

Thanx for your help Appreciate it all :)
 
I have had NO problem using toFixed(). Just tried it and it worked fine.

Code:
var amount = 10.5;
document.write(amount.toFixed(2));

Lee
 
Lee is correct - toFixed will always add the appropriate amount of 0s defined by the parameter you pass to the method.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
This op is being lucky till now just because they proliferate stars...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top