# minutes to decimal calculator javascript version

trying to do a more simple version of this post.

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

##### Related Search Terms

No Items Found.

Type in a Nick Name here