如何在输入中显示5位小数(math.round)



这是一个单元转换器示例。我想显示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>

使用to fixed((方法根据需要设置小数点数量。例如

const x=0.123234243;const y=x.to固定(5(

不幸的是,具有5位数字的toFixed方法在您的情况下没有帮助,因为它会自动对数字进行四舍五入,因此500/162500返回0.00308,而不是像您预期的那样返回0.00307,此外,对于81250/162500,它返回0.50000,而不是0.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

最新更新