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>
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Shahzad CR7
  • 125
  • 1
  • 1
  • 6
  • 4
    Take your pick: [`Math.round`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/round), [`Math.ceil`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/ceil), [`Math.floor`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/floor). – DCoder Sep 14 '12 at 11:39
  • possible duplicate of [How do I convert a float to an int in Javascript?](http://stackoverflow.com/questions/596467/how-do-i-convert-a-float-to-an-int-in-javascript) – DCoder Sep 14 '12 at 11:47
  • When you say "still does not work" it would be more informative to tell what you get - error or worng value or what? – mplungjan Sep 14 '12 at 12:05
  • You're first question was about multiplying floating point numbers with integers, and now it's about parsing strings, making some of these answers irrelevant. – Aesthete Sep 14 '12 at 12:51
  • possible duplicate of [Remove decimal from JavaScript number](http://stackoverflow.com/questions/7641818/remove-decimal-from-javascript-number) – Foreever Apr 24 '14 at 10:51
  • consider: parseInt( 2.3*100 ) === 229 – Yin Feb 18 '16 at 09:37

8 Answers8

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
vzwick
  • 11,008
  • 5
  • 43
  • 63
  • 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 '12 at 11:54
  • check how to change that in function – Shahzad CR7 Sep 14 '12 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 '12 at 14:08
  • Consider the following, `"foo" | 0` and `"0.1234" | 0` both return `0`. `Math.floor("foo")` returns `NaN` and `Math.floor("0.1234")` returns 0. This is a significant difference. – Aesthete Sep 14 '12 at 14:17
  • 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 '16 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. ;) – James Wilkins Sep 30 '17 at 20:39
9

You should use JavaScript's parseInt()

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 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()`. – David Thomas Sep 14 '12 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 '16 at 14:21
5

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

 Math.trunc(345.99933)

enter image description here

ALLSYED
  • 1,523
  • 17
  • 15
4
var num = 233.256;
console.log(num.toFixed(0));

//output 233
g1ji
  • 1,099
  • 1
  • 10
  • 21
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)
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • 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 '12 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));
Aesthete
  • 18,622
  • 6
  • 36
  • 45
  • still not working for me basically i am setting these value on graph bar – Shahzad CR7 Sep 14 '12 at 11:48
  • basically prescript.values is text type field not number i think that may be the issue – Shahzad CR7 Sep 14 '12 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 '12 at 11:53
  • And add a radix `ans7 = Math.floor(.17*parseInt(prescription.value,10)*parseInt(cost.value,10));` – mplungjan Sep 14 '12 at 12:08
  • i have added code how to change that num value to withoud decimal – Shahzad CR7 Sep 14 '12 at 12:10
0

Have you try to get value using parseInt

Try :

console.log(parseInt(ans7));
Jignesh Rajput
  • 3,538
  • 30
  • 50
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>
Penny Liu
  • 15,447
  • 5
  • 79
  • 98