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
https://forums.phpfreaks.com/topic/57508-leading-zeros-in-total-needed/
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;
}

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.

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;
}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.