5

I am using numeric in an HTML web page. 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>

6

8 Answers 8

24

Assuming you just want to truncate the decimal part (no rounding), here's a shorter (and less expensive) alternative to parseInt() or 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:

10     | 0 // => 10
10.001 | 0 // => 10
10.991 | 0 // => 10
"10"   | 0 // => 10
"10.1" | 0 // => 10
"10.9" | 0 // => 10
6
  • Should we really assume that speed is an issue an an operation like this? It's faster than floor because floor handles multiple types. Floor is also explicit in it's intentions. Novel approach though, +1.
    – Aesthete
    Sep 14, 2012 at 11:54
  • check how to change that in function Sep 14, 2012 at 12:11
  • @Aesthete "Floor handles multiple types" – so does the bitwise OR 0: 10|0 => 10, 10.1|0 => 10, "10"|0 => 10, "10.1"|0 => 10 … Semantically, floor() is better suited, of course.
    – vzwick
    Sep 14, 2012 at 14:08
  • 1
    @Jake - Thanks for pointing this out. The problem isn't in the approach per se, but due to floating point arithmetics: 2.3 * 100 = 229.99999999999997. I added a disclaimer to the answer nonetheless.
    – vzwick
    Feb 25, 2016 at 14:18
  • 2
    @Jake This issue can easily be solved for some cases by adding a SMALL fraction to give a little "push" to numbers - assuming you're only dealing with positive numbers. For example, ((2.3+0.00000000001)*100)|0 === 230 just fine. For all others it simply adds such a small amount that nothing much happens. I'm building a simulation engine where speed is important, and which does not need perfect precision, and I think this works great for my needs. ;) Sep 30, 2017 at 20:39
9

You should use JavaScript's parseInt()

2
  • He already is; albeit he's not using the radix for some reason...though I'm curious as to why he hasn't simply re-used parseInt(). Sep 14, 2012 at 11:40
  • 1
    @Jake - parseInt works as intended. It's 2.3 * 100 that doesn't yield what you would expect.
    – vzwick
    Feb 25, 2016 at 14:21
5

In ES6 , you can use builtin method trunc from Math Object

 Math.trunc(345.99933)

enter image description here

4
var num = 233.256;
console.log(num.toFixed(0));

//output 233
1
  • Don't use this. 233.9.toFixed(0) returns 234 not 233.
    – Zied Hf
    Mar 22, 2023 at 0:03
2

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)
1
  • 1
    toFixed has various issues (e.g. (0.595).toFixed(2) gives 0.59 in Firefox, 0.60 in IE), better to use Math.round or similar.
    – RobG
    Sep 14, 2012 at 12:03
1

Mathematically, using a floor function makes the most sense. This gives you a real number to the largest previous integer.

ans7 = Math.floor(.17*parseInt(prescription.values)*parseInt(cost.value));
5
  • still not working for me basically i am setting these value on graph bar Sep 14, 2012 at 11:48
  • basically prescript.values is text type field not number i think that may be the issue Sep 14, 2012 at 11:50
  • @ShahzadBaloch - Are you sure it's because you're looking for one property called value and the other called values? If either of these values is undefined you're going to get NaN returned. Otherwise is you multiple a float by two ints and floor the result it will always work.
    – Aesthete
    Sep 14, 2012 at 11:53
  • And add a radix ans7 = Math.floor(.17*parseInt(prescription.value,10)*parseInt(cost.value,10));
    – mplungjan
    Sep 14, 2012 at 12:08
  • i have added code how to change that num value to withoud decimal Sep 14, 2012 at 12:10
0

Have you try to get value using parseInt

Try :

console.log(parseInt(ans7));
0

~~ operator is a faster substitute for 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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.