【问题标题】:How to remove digits after decimal using JavaScript?如何使用 JavaScript 删除小数点后的数字?
【发布时间】:2012-09-07 13:45:58
【问题描述】:

我在 HTML 网页中使用数字。问题是我想要没有小数的数字。

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>

【问题讨论】:

标签: javascript


【解决方案1】:

假设您只想截断小数部分(无舍入),这里有一个更短(且更便宜)的替代 parseInt()Math.floor()

var number = 1.23;
var nodecimals = number | 0; // => 1

带有intfloatstring 输入的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

【讨论】:

  • 我们真的应该假设速度是这样一个操作的问题吗?它比 floor 快,因为 floor 处理多种类型。 Floor 的意图也很明确。不过,这是一种新颖的方法,+1。
  • 检查如何在函数中更改
  • @Aesthete "Floor 处理多种类型" – 按位 OR 0 也是如此:10|0 =&gt; 1010.1|0 =&gt; 10"10"|0 =&gt; 10"10.1"|0 =&gt; 10 ... 从语义上讲,floor() 更适合当然。
  • @Jake - 感谢您指出这一点。问题不在于方法本身,而在于浮点运算:2.3 * 100 = 229.99999999999997。尽管如此,我还是在答案中添加了免责声明。
  • @Jake 在某些情况下,这个问题可以很容易地解决,方法是添加一个小分数来稍微“推动”数字——假设你只处理正数。例如, ((2.3+0.00000000001)*100)|0 === 230 就好了。对于所有其他人来说,它只是添加了如此少的数量,以至于没有发生任何事情。我正在构建一个速度很重要的模拟引擎,它不需要完美的精度,我认为这非常适合我的需求。 ;)
【解决方案2】:

你应该使用 JavaScript 的parseInt()

【讨论】:

  • 他已经是了;尽管出于某种原因他没有使用radix...尽管我很好奇他为什么不简单地重新使用parseInt()
  • @Jake - parseInt 按预期工作。 2.3 * 100 并没有达到您的预期。
【解决方案3】:

在 ES6 中,您可以使用 Math Object 的内置方法 trunc

 Math.trunc(345.99933)

【讨论】:

    【解决方案4】:
    var num = 233.256;
    console.log(num.toFixed(0));
    
    //output 233
    

    【讨论】:

      【解决方案5】:

      返回字符串:

      (.17*parseInt(prescription.values)*parseInt(cost.value)).toFixed(0);
      

      返回整数:

      Math.round(.17*parseInt(prescription.values)*parseInt(cost.value));
      

      在解析整数时记得使用基数:

      parseInt(cost.value, 10)
      

      【讨论】:

      • toFixed 有各种问题(例如,(0.595).toFixed(2) 在 Firefox 中为 0.59,在 IE 中为 0.60),最好使用 Math.round 或类似的。
      【解决方案6】:

      在数学上,使用floor 函数是最有意义的。这会给你一个实数到最大的前一个整数。

      ans7 = Math.floor(.17*parseInt(prescription.values)*parseInt(cost.value));
      

      【讨论】:

      • 仍然不适合我基本上我在图表栏上设置这些值
      • 基本上 prescript.values 是文本类型字段而不是数字,我认为这可能是问题
      • @ShahzadBaloch - 你确定这是因为你正在寻找一个名为 value 的属性和另一个名为 values 的属性吗?如果这些值中的任何一个未定义,您将返回 NaN。否则,您是否将浮点数乘以两个整数,然后将结果设为总是工作。
      • 并添加基数ans7 = Math.floor(.17*parseInt(prescription.value,10)*parseInt(cost.value,10));
      • 我已经添加了如何将 num 值更改为不带小数点的代码
      【解决方案7】:

      您是否尝试使用parseInt 获得价值

      试试看:

      console.log(parseInt(ans7));
      

      【讨论】:

        【解决方案8】:

        ~~ 运算符是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>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-20
          • 2019-09-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多