删除 Javascript 中小数点后的数字
Remove digits after decimal in Javascript
我正在制作一个从 GPS 度数、十进制分到度、分、秒的转换器,因为我在 Internet 上找不到。
能否告诉我如何去除计算总计字段中小数点后的数字?
<HEAD>
<TITLE>Javascript Calculator</TITLE>
<SCRIPT language="javascript" type="text/javascript">
function multiply()
{
a=Number(document.calculator.number1.value);
b=.060;
c=a*b;
document.calculator.total.value=c;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="calculator">
Decimal: <INPUT type="text" name="number1"> <br>
<INPUT type="button" value="Calculate" onclick="javascript:multiply();"><br>
Seconds: <INPUT type="text" name="total">
</FORM>
</BODY>
Math.round(x) //x being your number you wish to 'round
JSFiddle : http://jsfiddle.net/j37m78a4/2/
我已经注释掉提供小数点值答案的代码。取消注释并注释掉它下面的行以查看原始答案是什么。 :)
如果您不知道将代码放在哪里,我建议您先阅读 Javascript 一本书或浏览在线教程以了解基础知识。从直接进入一个项目开始是好的,但在这样做之前需要了解基础知识。 :)
最快的是使用右移运算符的按位移位。 c = 5.674447>>0
以下内容应该适合您。改变了你原来的一些东西,但这只是因为 id 比 name 更好用,因为 name 带来了一个节点列表。
<HEAD>
<TITLE>Javascript Calculator</TITLE>
</HEAD>
<BODY>
<FORM name="calculator">Decimal:
<INPUT type="text" id="number1">
<br>
<INPUT type="button" value="Calculate" id="calculate">
<br>Seconds:
<INPUT type="text" id="total">
</FORM>
<script>
// put your script tags at the bottom of the page
// first get the calculate button by its Id.
var calculate = document.getElementById('calculate');
// then add an event listener to it that calls multiply function when clicked
calculate.addEventListener('click', multiply, false);
// this multiply function is called when the button is clicked
function multiply() {
// get the inputs value then the total input fields by id
var a = document.getElementById('number1').value;
var total = document.getElementById('total');
var b = 0.060;
// Math.floor is used to round down since you just want to remove the numbers after decimal.
var c = Math.floor(a * b);
// set the value of total
total.value = c;
}
</script>
</BODY>
我正在制作一个从 GPS 度数、十进制分到度、分、秒的转换器,因为我在 Internet 上找不到。
能否告诉我如何去除计算总计字段中小数点后的数字?
<HEAD>
<TITLE>Javascript Calculator</TITLE>
<SCRIPT language="javascript" type="text/javascript">
function multiply()
{
a=Number(document.calculator.number1.value);
b=.060;
c=a*b;
document.calculator.total.value=c;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="calculator">
Decimal: <INPUT type="text" name="number1"> <br>
<INPUT type="button" value="Calculate" onclick="javascript:multiply();"><br>
Seconds: <INPUT type="text" name="total">
</FORM>
</BODY>
Math.round(x) //x being your number you wish to 'round
JSFiddle : http://jsfiddle.net/j37m78a4/2/
我已经注释掉提供小数点值答案的代码。取消注释并注释掉它下面的行以查看原始答案是什么。 :)
如果您不知道将代码放在哪里,我建议您先阅读 Javascript 一本书或浏览在线教程以了解基础知识。从直接进入一个项目开始是好的,但在这样做之前需要了解基础知识。 :)
最快的是使用右移运算符的按位移位。 c = 5.674447>>0
以下内容应该适合您。改变了你原来的一些东西,但这只是因为 id 比 name 更好用,因为 name 带来了一个节点列表。
<HEAD>
<TITLE>Javascript Calculator</TITLE>
</HEAD>
<BODY>
<FORM name="calculator">Decimal:
<INPUT type="text" id="number1">
<br>
<INPUT type="button" value="Calculate" id="calculate">
<br>Seconds:
<INPUT type="text" id="total">
</FORM>
<script>
// put your script tags at the bottom of the page
// first get the calculate button by its Id.
var calculate = document.getElementById('calculate');
// then add an event listener to it that calls multiply function when clicked
calculate.addEventListener('click', multiply, false);
// this multiply function is called when the button is clicked
function multiply() {
// get the inputs value then the total input fields by id
var a = document.getElementById('number1').value;
var total = document.getElementById('total');
var b = 0.060;
// Math.floor is used to round down since you just want to remove the numbers after decimal.
var c = Math.floor(a * b);
// set the value of total
total.value = c;
}
</script>
</BODY>