Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 870 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to truncate floating point number in jscript?

#1
How to truncate float value in Jscript?

eg.

var x = 9/6

Now x contains a floating point number and it is 1.5.
I want to truncate this and get the value as 1
Reply

#2
x = Math.floor(x)

This will round the value of x down to the nearest integer below.
Reply

#3
[`Math.round()`][1] should achieve what you're looking for, but of course it'll round `1.5` to `2`. If you always want to round down to the nearest integer, use [`Math.floor()`][2]:

var x = Math.floor(9 / 6);


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
`Math.floor()` only works as the OP intended when the number is positive, as it rounds *down* and not towards zero. Therefore, for negative numbers, `Math.ceil()` must be used.

var x = 9/6;
x = (x < 0 ? Math.ceil(x) : Math.floor(x));
Reply

#5
Another solution could be:

~~~javascript
var x = parseInt(9 / 6);
Wscript.StdOut.WriteLine(x); // returns 1
~~~

the main purpose of the `parseInt()` function is to parse strings to integers. So I guess it might be slower than `Math.floor()` and methods as such.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through