[英]How to remove digits after decimal using JavaScript?
I am using numeric in an HTML web page.我在 HTML 网页中使用数字。 The problem is that I want numbers without decimals.问题是我想要没有小数的数字。
function copyText() { var mynumber = document.getElementById("field1").value; alert(mynumber); var mytest = parseInt(mynumber); }
Field1: <input type="number" id="field1" value="123.124" /><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>
Assuming you just want to truncate the decimal part (no rounding), here's a shorter (and less expensive) alternative to parseInt()
or Math.floor()
:假设您只想截断小数部分(不舍入),这里有一个更短( parseInt()
便宜)的替代parseInt()
或Math.floor()
:
var number = 1.23;
var nodecimals = number | 0; // => 1
Further examples for the bitwise OR 0
behavior with int
, float
and string
input:使用int
、 float
和string
输入的bitwise OR 0
行为的更多示例:
10 | 0 // => 10
10.001 | 0 // => 10
10.991 | 0 // => 10
"10" | 0 // => 10
"10.1" | 0 // => 10
"10.9" | 0 // => 10
你应该使用 JavaScript 的parseInt()
var num = 233.256;
console.log(num.toFixed(0));
//output 233
returns string:返回字符串:
(.17*parseInt(prescription.values)*parseInt(cost.value)).toFixed(0);
returns integer:返回整数:
Math.round(.17*parseInt(prescription.values)*parseInt(cost.value));
Remember to use radix when parsing ints:解析整数时记得使用基数:
parseInt(cost.value, 10)
Mathematically, using a floor
function makes the most sense.从数学上讲,使用floor
函数最有意义。 This gives you a real number to the largest previous integer.这给你一个实数到最大的前一个整数。
ans7 = Math.floor(.17*parseInt(prescription.values)*parseInt(cost.value));
Have you try to get value using parseInt
您是否尝试使用parseInt
获取价值
Try :尝试 :
console.log(parseInt(ans7));
~~
operator is a faster substitute for Math.floor()
. ~~
运算符是Math.floor()
的更快替代品。
function copyText() { var mynumber = document.getElementById("field1").value; alert(~~mynumber); }
<fieldset> <legend>Field1</legend> <input type="number" id="field1" value="123.124" /> <button onclick="copyText()">Check Number</button> </fieldset>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.