this is a unit converter example. I want to show 5 decimals but ı'm new on js and can't solve it...
For example, people want to convert k to m it's okay because of big numbers. But try it in reverse (m to k) basicly input never change till user write minimum 162500. For example ı want to show in input as a output 0.5. When people write 81250, they should get 0.5. When user write 500, they could get 0.00307
I hope ı can explain myself.
Javascript
var firstInput = document.getElementById('firstInput') , secondInput = document.getElementById("secondInput") ,
firstVar , secondVar , weightButton = document.getElementById('weightButton') , firstText = document.getElementById('firstText')
secondText = document.getElementById("secondText");
// the calculating functions of Distance
function fromKtoM(){
secondInput.value = (Math.round(parseFloat(firstInput.value) * 162500)) || 0.000000 ;
};
function fromMtoK(){
firstInput.value = (Math.round(parseFloat(secondInput.value) / 162500 )) || 0.000000 ;
};
// other functions
function changeOther(){
firstText.innerHTML = 'Radian' ;
secondText.innerHTML = "Degree" ;
firstInput.oninput = fromRtoD ;
secondInput.oninput = fromDtoR ;
firstInput.value = '' ;
secondInput.value = '' ;
};
function changeOther1(){
firstText.innerHTML = 'MpH' ;
secondText.innerHTML = "KpH" ;
firstInput.oninput = fromMtoKp ;
secondInput.oninput = fromkptoM ;
firstInput.value = '' ;
secondInput.value = '' ;
};
function changeOther2(){
firstText.innerHTML = 'Liter' ;
secondText.innerHTML = "Gallon" ;
firstInput.oninput = fromLtoG ;
secondInput.oninput = fromGtoL ;
firstInput.value = '' ;
secondInput.value = '' ;
};
// weight functions
function change(){
firstText.innerHTML = 'Kilograme' ;
secondText.innerHTML = "Pound" ;
firstInput.oninput = fromKtoP ;
secondInput.oninput = fromPtoK ;
firstInput.value = '' ;
secondInput.value = '' ;
} ;
function change1(){
firstText.innerHTML = 'Kilograme' ;
secondText.innerHTML = "Ounce" ;
firstInput.oninput = fromKtoO ;
secondInput.oninput = fromOtoK ;
firstInput.value = '' ;
secondInput.value = '' ;
} ;
function change2(){
firstText.innerHTML = 'Gram' ;
secondText.innerHTML = "Ounce" ;
firstInput.oninput = fromGtoO ;
secondInput.oninput = fromOtoG ;
firstInput.value = '' ;
secondInput.value = '' ;
} ;
function change3(){
firstText.innerHTML = 'Pound' ;
secondText.innerHTML = "Ounce" ;
firstInput.oninput = fromPtoO ;
secondInput.oninput = fromOtoP ;
firstInput.value = '' ;
secondInput.value = '' ;
} ;
// distance functions
function changeDistance(){
firstText.innerHTML = 'Kilometer' ;
secondText.innerHTML = "Mile";
firstInput.oninput = fromKtoM ;
secondInput.oninput = fromMtoK ;
firstInput.value = '' ;
secondInput.value = '' ;
};
function changeDistance1(){
firstText.innerHTML = 'Centimeter' ;
secondText.innerHTML = "Inch";
firstInput.oninput = fromCtoI ;
secondInput.oninput = fromItoC ;
firstInput.value = '' ;
secondInput.value = '' ;
};
function changeDistance2(){
firstText.innerHTML = 'Meter' ;
secondText.innerHTML = "Yard";
firstInput.oninput = fromMtoY ;
secondInput.oninput = fromYtoM ;
firstInput.value = '' ;
secondInput.value = '' ;
};
function changeDistance3(){
firstText.innerHTML = 'Meter' ;
secondText.innerHTML = "Feet";
firstInput.oninput = fromMtoF ;
secondInput.oninput = fromFtoM ;
firstInput.value = '' ;
secondInput.value = '' ;
};
function changeDistance4(){
firstText.innerHTML = 'Centimeter' ;
secondText.innerHTML = "Feet";
firstInput.oninput = fromCtoFe ;
secondInput.oninput = fromFetoC ;
firstInput.value = '' ;
secondInput.value = '' ;
};
function changeDistance5(){
firstText.innerHTML = 'Inch' ;
secondText.innerHTML = "Feet";
firstInput.oninput = fromItoF ;
secondInput.oninput = fromFtoI ;
firstInput.value = '' ;
secondInput.value = '' ;
};
// digree functions
function changeDigree(){
firstText.innerHTML = 'Celsius' ;
secondText.innerHTML = "Fahrenheit";
firstInput.oninput = fromCtoF ;
secondInput.oninput = fromFtoC ;
firstInput.value = '' ;
secondInput.value = '' ;
};
function changeDigree1(){
firstText.innerHTML = 'Fahrenheit' ;
secondText.innerHTML = "Kelvin";
firstInput.oninput = fromFtoK ;
secondInput.oninput = fromKtoF ;
firstInput.value = '' ;
secondInput.value = '' ;
} ;
function changeDigree2(){
firstText.innerHTML = 'Kelvin' ;
secondText.innerHTML = "Celecuis";
firstInput.oninput = fromKtoC ;
secondInput.oninput = fromCtoK ;
firstInput.value = '' ;
secondInput.value = '' ;
} ;
// when clicking buttons this functions get called
// distance button
$("#distanceButton").on("click" , changeDistance) ;
$("#distanceButton1").on("click" , changeDistance1) ;
$("#distanceButton2").on("click" , changeDistance2) ;
$("#distanceButton3").on("click" , changeDistance3) ;
$("#distanceButton4").on("click" , changeDistance4) ;
$("#distanceButton5").on("click" , changeDistance5) ;
// digree buttons
$("#digreeButton").on("click" , changeDigree);
$("#digreeButton1").on("click" , changeDigree1);
$("#digreeButton2").on("click" , changeDigree2);
// weight buttons
$('#weightButton').on('click' , change) ;
$('#weightButton1').on('click' , change1) ;
$('#weightButton2').on('click' , change2) ;
$('#weightButton3').on('click' , change3) ;
// other buttons
$('#otherButton').on('click' , changeOther) ;
$('#otherButton1').on('click' , changeOther1) ;
$('#otherButton2').on('click' , changeOther2) ;
// This is the clear button function
$('#clear').on('click' , function(){
firstInput.value = '' ;
secondInput.value = '' ;
} ) ;
// initital Config is km/mile
firstInput.oninput = fromKtoM ;
secondInput.oninput = fromMtoK ;
// responsive for other devices
$("button").not(document.getElementById( "clear" )).addClass('btn waves-effect waves-light blue-grey darken-2') ;
HTML
<form id='form1' class='row'>
<div id="formm1"><p id='firstText' style='margin-bottom: 0px; color: white; font-size: 12px; font-weight: 200; font-family: 'Source Sans Pro', sans-serif;'>From</p><input id='firstInput' type='number' step="0.01" pattern="^\d (?:\.\d{1,2})?$" value="0" placeholder="0.00" /></div>
<div id="formm2"><p id='secondText' style='margin-bottom: 0px; color: white; font-size: 12px; font-weight: 200; font-family: 'Source Sans Pro', sans-serif;'>To</p><input id='secondInput' type='number' step="0.01" pattern="^\d (?:\.\d{1,2})?$" value="0" /></div>
</form>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/js/materialize.min.js'></script><script src="./script.js"></script>
CodePudding user response:
use to fixed() method to set number of decimal points as desired. for example,
const x = 0.123234243; const y = x.toFixed(5)
CodePudding user response:
Unfortunately the toFixed
method with 5 digits cannot help in your case because it automatically rounds numbers so 500 / 162500 returns 0.00308 and not 0.00307 like you expect, moreover for 81250 / 162500 it returns 0.50000 and not 0.5. You can instead convert the result of the division to a string and slice all the digits after the .
that are beyond the fifth digit:
function fromMtoK(n) {
const str = (n / 162500).toString();
return str.slice(0, str.indexOf('.') 1 5);
};
console.log(fromMtoK(81250)); //<-- prints 0.5
console.log(fromMtoK(500)); // <-- prints 0.00307
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>