minutes to decimal calculator javascript version

trying to do a more simple version of this post.

https://kruxor.com/view/code/NIjAQ/quick-tool-to-convert-time-into-decimal/ 

the php version requires post back to the server, so i think this can be done much easier in javascript.

i think the round function may need to change for javascript. rather than using round, need to use the Math.round(num) function.

Here is the function that will return the decimal version of minutes.

Javascript

function calc_decimal(minutes_val) {
  return Math.round(minutes_val) / 60;
}

so its working in console, just add: 

Javascript

console.log(calc_decimal(45));

which will return 45 minutes in decimal which is 0.75

now need to add a html form to allow users to easily change the minute value

HTML

<h2>Calculate Minutes to Decimal</h2>
<div class='form-row'>
	<input type='number' name='minutes_val' id='minutes_val'>
  	<button class='btn btn-primary' onclick='calc_decimal_click();'>Calculate Decimal</button>
</div>
<div class='alert alert-primary mt20'>
  <div id='dec_result'></div>
</div>

Javascript

function calc_decimal(minutes_val) {
  return Math.round(minutes_val) / 60;
}
console.log(calc_decimal(45));

function calc_decimal_click() {
  // set a html value here for the result rather than returning it. 
  var minutes_val = document.getElementById("minutes_val").value;
  console.log("minutes_val : " + minutes_val);
  var result = Math.round(minutes_val) / 60;
  console.log("result : " + result);
  // build a nice result string
  var res_string = minutes_val + " Minutes displayed as a Decimal is " + result
  document.getElementById("dec_result").innerHTML = res_string;
}

Calculate Minutes to Decimal

Other Items in javascript

No Items Found.

Add Comment
Type in a Nick Name here
 
Page Views

This page has been viewed 444 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

โค๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ•น

Random Quote
There is a qualitative and quantitative difference between a day that begins with a little exercise, a book, meditation, a good meal, a thoughtful walk, and the start of a day that begins with a smartphone in bed.
Unknown