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
  • 4
    Take your pick: Math.round, Math.ceil, Math.floor. Commented Sep 14, 2012 at 11:39
  • possible duplicate of How do I convert a float to an int in Javascript? Commented Sep 14, 2012 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? Commented Sep 14, 2012 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. Commented Sep 14, 2012 at 12:51
  • possible duplicate of Remove decimal from JavaScript number Commented Apr 24, 2014 at 10:51

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
Sign up to request clarification or add additional context in comments.

6 Comments

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.
check how to change that in function
@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.
@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.
@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. ;)
|
9

You should use JavaScript's parseInt()

2 Comments

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().
@Jake - parseInt works as intended. It's 2.3 * 100 that doesn't yield what you would expect.
5

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

 Math.trunc(345.99933)

enter image description here

Comments

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

//output 233

1 Comment

Don't use this. 233.9.toFixed(0) returns 234 not 233.
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 Comment

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.
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 Comments

still not working for me basically i am setting these value on graph bar
basically prescript.values is text type field not number i think that may be the issue
@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.
And add a radix ans7 = Math.floor(.17*parseInt(prescription.value,10)*parseInt(cost.value,10));
i have added code how to change that num value to withoud decimal
0

Have you try to get value using parseInt

Try :

console.log(parseInt(ans7));

Comments

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>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.