francoom
francoom

Reputation: 121

How do I show decimals in Javascript that adds values from text boxes?

I have a javascript (combined with ColdFusion for looping) that automatically adds values from a textbox. How can it display decimal format?

<script language="javascript">
function getValues(val){
<cfloop from=1 to=3 index="j">
var numVal#j#=parseInt(document.getElementById("#j#").value);
</cfloop>

var totalValue = 
<cfloop from=1 to=3 index="k">
numVal#k# + 
</cfloop>
0;

document.getElementById("main").value = totalValue;
}
</script>

Result:

<input type="text" id="main" value="24" readonly>

Upvotes: 0

Views: 1219

Answers (1)

PSL
PSL

Reputation: 123739

In order to convert to decimal format(say two decimal places) you can use the function as below.

if totalValue is a string representing numerical value:-

document.getElementById("main").value = parseFloat(totalValue, 10).toFixed(2);

if totalValue is already a number then you can simply do totalValue.toFixed(2); Not too sure about ColdFusion syntax but seems like you are already getting a number so you may go with the second approach.

http://jsfiddle.net/J5Lur/

Upvotes: 1

Related Questions