Showing posts with label Kilograms To Pounds Converter in Javascript. Show all posts
Showing posts with label Kilograms To Pounds Converter in Javascript. Show all posts

Sunday, December 11, 2016

Kilograms To Pounds Converter in JavaScipt

Here is a sample program that will ask the user to give weight in kilograms and then convert in pounds using JavaScript. The code is very easy to understand and use.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.





Sample Program Output


Program Listing

<html>
<title> Kilograms To Pounds Converter 1.0
</title>
<body bgcolor="lightblue">
<font color="black" face="Arial" size="5">
<center>
<br>
    Kilograms To Pounds Converter 1.0
<br>
</center>
</font>
<style type="text/css">
.labelText {
 text-align : right;
 padding-right : 3px;
 color   : black;
 font-family:"Arial";
 font-size    : 18px;
}
</style>

 <form  name="mass" method="post">
<table>
<tr>
<tr>
 <td> &nbsp; </td>
 </tr>
<td class="labelText">  Enter Weight in Kilograms

</td>
<td> <input type="text" name="kg"  size="10"/> </td>
</tr>

 <tr>
 <td> &nbsp; </td>
 </tr>
</td>
<tr>
<td class="labelText">
The result is
</td>
<td> 
<input type="text" name="display"  size="50"/> </td>
 </tr>
  <tr>
 <td> &nbsp; </td>
 </tr>
</td>                   
<td> <input type="button" value="Compute" 
title="Click here to compute the weight in pounds."
 onClick="compute1()">
</td> 
<td> <input type="button" value="Clear" 
title="Click here to clear the text box."
 onClick="clear_all()">
</td> 

</table>
</font>
</form>
<script language ="javascript">
function compute1()
{


var value1, solve1,final_result;
  
value1 = parseInt(document.mass.kg.value);
              
solve1 = (value1 * 2.2);
// For two decimal places convertion
 final_result = Math.round(solve1 * 100) /100;

text1=" kg = ";
text3=" kilograms multiplied by 2.2 = ";
text4=" pounds.";


result = value1 + text3 + final_result + text4; 
               

document.mass.display.value = result;
}

function clear_all()
{
document.mass.kg.value = "";
document.mass.display.value = "";
document.mass.kg.focus(); 
}
</script>
</body>
</html>


Wednesday, September 16, 2015

Kilograms To Pounds Converter in Javascript

In this article I will show you how use Javascript to convert kilograms to pounds . In the code I included a function that will only accept numeric value and not allow characters like letters and special characters. I hope you will find my work useful and beneficial in learning how to program in Javascript.

If you  have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.






Sample Program Output


Program Listing

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title> Kilograms To Pounds Converter in Javascript</title>
</head>
<style>
body,label {
     background-color:lightgreen;
     font-family:arial;
     color:blue;
     font-size:18px;
     font-weight:bold;
     }
label{
    display: table-cell;
    text-align: right;
}
input {
  display: table-cell;
}
div.row{
    display:table-row;
}
button {
    font-family: 'arial';
    font-size:18px;
    color:red;
}
input[type="number"] { 
    font-family: 'arial';
    font-size:18px;
    color:red;
    height: 25px;
    width: 38px;
}
</style>
<body>
<h2> Kilograms To Pounds Converter in Javascript </h2>
 <div class="row"><label>Enter a value to convert</label>
      &nbsp;&nbsp;&nbsp;
      <input type="number" size="5" id="values" maxlength="5" onkeypress="return isNumber(event)" 
      required autofocus><br></div>
  <br>
<button title="Click here to convert to pounds.">Convert To Pounds</button>
&nbsp;&nbsp;&nbsp;&nbsp;
<button title="Click here to clear the text box.">Clear</button><br>
<br><br>
<div id="result"></div>
<script>
var button = document.getElementsByTagName('button');

button[0].onclick = display_result;
button[1].onclick = clear_text;

document.onload = function() {
   document.getElementById('values').focus();
};
   // Function to convert kilogram(s) to pound(S)
function convert_to_pounds(number_value)
{
   
  return (number_value *  2.20462262 );
}

// This function will display the pounds equivalent
function display_result() {
var kilograms = document.getElementById("values").value;
var result = convert_to_pounds(kilograms);
 
if (document.getElementById("values").value == '') {
       alert("Cannot be empty. Please put a numerical value.");
       document.getElementById('values').focus();
       
   }
else {
 document.getElementById('result').innerHTML = kilograms + " kilogram(s) is equal to "
       + result.toFixed(2) + " pound(s). ";
}
}
// This function will clear the text box.
function clear_text() {
document.getElementById("values").value="";
document.getElementById("result").innerHTML="";
document.getElementById("values").focus();
}

// This function will only allow numerical value in the text box.
function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}
</script>
</body>
</html>