Jump to content

Leading Zeros in Total Needed.


TRI0N

Recommended Posts

Okay here is a simple script that will add up a running total.  My problem is that I want it to output leading zeros at the end.

 

Example: Its putting out 24 when I want it to be 24.00 or if there is cents in this to show it as.  24.50 not 24.5.

 

function addMe(){ 
v1=document.getElementById('rounds').value 
v2=document.getElementById('players').value 
v3=document.getElementById('cart').value
v4=document.getElementById('room_rate').value
v5=document.getElementById('no_nights').value

wd_rate=24.00
we_rate=29.00
c_rate=15.00

if(document.getElementById('rounds').value == 1){
vT1=(Number(v2) * Number(wd_rate)) 
}else{
vT1=(Number(v2) * Number(wd_rate)) - Number(v1)
}
if(document.getElementById('cart').value > 0){
vT2=(Number(v2) * Number(c_rate)) 
}else{
vT2=0.00
}
vT3=(Number(v4) * Number(v5)) 

vT=Number(vT1) + Number(vT2) + Number(vT3)

document.getElementById('tot').innerHTML=vT ; 

}

 

Thanks for any help on this!

 

Link to comment
Share on other sites

try something like this:

 

function pad(num) {
var result = num;

if (result.indexOf('.') != -1) {
	var tmp[] = num.split('.');
	var suffix = tmp[1];

	if (suffix.length < 1) {
		suffix = "00";
	} else if (suffix.length == 1) {
		suffix += "0";
	} else if (suffix.length > 2) {
		suffix = suffix.substring(0, 2);
	}

	result = tmp[0] + "." + suffix;
} else {
	result += ".00";
}

return result;
}

Link to comment
Share on other sites

Not sure how to convert what you got there but let take vT and see how would I run the given script above a way to test that varible.  I tried a few ideas but they didn't work.

 

Perhaps if somone tossed me a bone on vT as an example before it is put back into a HTML Id value.

 

Thanks.

Link to comment
Share on other sites

The following should work then?

 

function pad(num) {
var result = vT;

if (result.indexOf('.') != -1) {
	var tmp[] = num.split('.');
	var suffix = tmp[1];

	if (suffix.length < 1) {
		suffix = "00";
	} else if (suffix.length == 1) {
		suffix += "0";
	} else if (suffix.length > 2) {
		suffix = suffix.substring(0, 2);
	}

	result = tmp[0] + "." + suffix;
} else {
	result += ".00";
}

document.getElementById('tot').innerHTML=result;
}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.