這是一個單位轉換器的例子。我想顯示 5 位小數,但我是 js 新手,無法解決它...
例如,人們想將 k 轉換為 m 沒關系,因為數字很大。但是嘗試反向(m 到 k)基本上輸入永遠不會改變,直到用戶寫入最小值 162500。例如,我想在輸入中顯示為輸出 0.5。當人們寫 81250 時,他們應該得到 0.5。當用戶寫 500 時,他們可以得到 0.00307
我希望我可以解釋自己。
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>
uj5u.com熱心網友回復:
使用到 fixed() 方法根據需要設定小數點數。例如,
常量 x = 0.123234243;const y = x.toFixed(5)
uj5u.com熱心網友回復:
不幸的是toFixed,5 位數的方法在您的情況下無濟于事,因為它會自動舍入數字,因此 500 / 162500 回傳 0.00308 而不是您期望的 0.00307,此外,對于 81250 / 162500,它回傳 0.50000 而不是 0.5。您可以將除法的結果轉換為字串,然后將 5 位之后的所有數字切片.:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/354972.html
標籤:javascript 数学 四舍五入
