我在HTML网页中使用数字。问题是我想要没有小数的数字。
<!DOCTYPE html>
<html>
<head>
//代码脚本
<script>
function copyText()
{
var mynumber=document.getElementById("field1").value;
alert(mynumber);
var mytest=parseInt(mynumber);
}
</script>
</head>
<body>
Field1: <input type="number" id="field1" value="123.124" /><br />
<br /><br />
<button onclick="copyText()">Check Number</button>
<p>A function is triggered when the button is clicked. The function copies the text in Field1 to Field2.</p>
</body>
</html>
//代码结束
答案 0 :(得分:21)
假设你只想截断小数部分(没有舍入),这里是parseInt()
或Math.floor()
的更短(且更便宜)的替代方案:
var number = 1.23;
var nodecimals = number | 0; // => 1
bitwise OR 0
,int
和float
输入的string
行为的更多示例:
10 | 0 // => 10
10.001 | 0 // => 10
10.991 | 0 // => 10
"10" | 0 // => 10
"10.1" | 0 // => 10
"10.9" | 0 // => 10
答案 1 :(得分:6)
您应该使用JavaScript的parseInt()
答案 2 :(得分:4)
答案 3 :(得分:1)
var num = 233.256;
console.log(num.toFixed(0));
//output 233
答案 4 :(得分:0)
返回字符串:
(.17*parseInt(prescription.values)*parseInt(cost.value)).toFixed(0);
返回整数:
Math.round(.17*parseInt(prescription.values)*parseInt(cost.value));
请记住在解析整数时使用radix:
parseInt(cost.value, 10)
答案 5 :(得分:0)
您是否尝试使用parseInt
尝试:
console.log(parseInt(ans7));
答案 6 :(得分:0)
在数学上,使用floor
函数最有意义。这为您提供了前一个最大整数的实数。
ans7 = Math.floor(.17*parseInt(prescription.values)*parseInt(cost.value));
答案 7 :(得分:0)
<div class="row">
<div class="col dd-meta">
<h5>Our Organization</h5>
<p>Our story, mission, team and more</p>
<a class="link-meta" href="javascript:;">Know more about us</a>
</div>
<div class="col dd-meta">
<ul class="link-common">
<li><a href="javascript:;">Action</a></li>
<li><a href="javascript:;">Action</a></li>
<li><a href="javascript:;">Action</a></li>
<li><a href="javascript:;">Action</a></li>
</ul>
</div>
<div class="col dd-meta">
<h5>Our Organization</h5>
<p>Our story, mission, team and more</p>
<a class="link-meta" href="javascript:;">Know more about us</a>
</div>
</div>
运算符可以更快地替代~~
。
Math.floor()
function copyText() {
var mynumber = document.getElementById("field1").value;
alert(~~mynumber);
}