• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2016
  • Last Modified:

how do I split a decimal number?

I have input boxes with the total hours for one given project and total hours worked for that day. I took the hours and minutes and split them up. I am trying to take the minutes and divide them by 60 to get the hour on the left side of the decimal and the minutes on the other side of the decimal that I plan to multiply by .06 to get the total minutes, but I cannot seem to get past the point of getting the decimal.

the code I need to get working is

var newMin = totMins2.split(".");
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language=javascript>
          function calculate(){
			   //var new3 = total2.Length;
			   //var newh = parseInt(new2.substring(0,2));
			   //var newm2 = parseInt(total2[1].substring(0,5));
			   var strNum = document.getElementById("timeTot").value;
			   var findColon = strNum.indexOf(':');
			   if (findColon<=1)
			   strNum = "0" + strNum;
			   var getMin = strNum.substring(findColon + 2, 5);
			   var getMin = strNum.substring(findColon + 1, 5);
			   var getHour = strNum.substring(0, 2);
			   var strNum2 = document.getElementById("dailyTot").value;
			   var findColon2 = strNum2.indexOf(':');
			   if (findColon2<=1)
			   strNum2 = "0" + strNum2;
			   var getMin2 = strNum2.substring(findColon2 + 2, 5);
			   var getMin2 = strNum2.substring(findColon2 + 1, 5);
			   var getHour2 = strNum2.substring(0, 2);
			   var totHours = parseInt(getHour*1) + parseInt(getHour2*1)
			   var totMins = parseInt(getMin*1) + parseInt(getMin2*1);
			   if (totMins <=59)
			   totMins = totMins;
			   totMins = parseFloat(totMins*1);
			   var totMins2 = totMins / 60;
			   var newMin = totMins2.split(".");
			   //var newHour = totMins[0].substring(0, 1)
			   //var newMin2 =  totMins[1].substring(0, 5)
<body onload="calculate();">
<form id="form2" name="form2" action="test.html" method="POST">
<input type='text' name="timeTot" id="timeTot" value="2:45" onBlur="if(timeTot.value!='' && dailyTot.value!='')calculate();">
<input type='text' name="dailyTot" id="dailyTot" value="5:45" onBlur="if(timeTot.value!='' && dailyTot.value!='')calculate();">

Open in new window

1 Solution

var newMin = totMins2.split(".");

You should be able to do...

alert(newMin[0]); //before the dot
alert(newMin[1]); //after the dot

Then you can multiply by .06 etc etc

bschave2Author Commented:
I am getting a object doesn't support the property or method error. on the var newMin = totMins2.split("."); code
you need to change the decimal number to a string to run the split method...

var tempTime = totMins2 + "";
var newMin = tempTime.split(".");
split is a string method. Try:
var newMin = String(totMins2).split(".");
bschave2Author Commented:
thanks very much....I am learning javascript as I go.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now