|
|
|
#1 (permalink) | |||||||||||
|
First Time Build
|
So I am doing a simple menu ordering for a javascript class but the only problem I have is that I need to add up three text fields which when you on click on a item it will have the cost (in one of the text fields) which contain decimals in them. I tryed parseFloat() and parseInt() but it doesn't work with decimals.
__________________Is their a way to add numbers that have decimals in them?
|
|||||||||||
|
|
|
|
|
#2 (permalink) | ||||||||||||
|
Programmer
|
You just have to convert them to decimals (doubles) right away.
Here is a full example Code:
<head>
<title>JS Example</title>
<script type="text/javascript">
function addThese(t1, t2, t3)
{
var v1 = parseFloat(document.getElementById(t1).value);
var v2 = parseFloat(document.getElementById(t2).value);
var v3 = parseFloat(document.getElementById(t3).value);
var total = v1+v2+v3;
document.getElementById('output').innerHTML = total;
}
</script>
</head>
<body>
<input id="Text1" type="text" /><br />
<input id="Text2" type="text" /><br />
<input id="Text3" type="text" /><br />
<input id="Button1" type="button" onclick="addThese('Text1','Text2','Text3')" value="button" />
<div id="output"></div>
</body>
</html>
__________________
"If there is a god, I hope he has a good excuse" Woody Allen
|
||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||
|
First Time Build
|
Thanks for your help.
__________________+1 for helping me out.
|
|||||||||||
|
|
|
|
|
#4 (permalink) | ||||||||||||
|
Programmer
|
No problem, glad to help out!
__________________
"If there is a god, I hope he has a good excuse" Woody Allen
|
||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|