Monday 25 December 2017

syntax - How do I convert a float number to a whole number in JavaScript?

itemprop="text">

I'd like to convert a float to a whole
number in JavaScript. Actually, I'd like to know how to do BOTH of the standard
conversions: by truncating and by rounding. And efficiently, not via converting to a
string and parsing.


itemprop="text">
class="normal">Answer





var intvalue =
Math.floor( floatvalue );
var intvalue = Math.ceil( floatvalue );

var intvalue = Math.round( floatvalue );

// `Math.trunc`
was added in ECMAScript 6
var intvalue = Math.trunc( floatvalue
);


href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math"
rel="noreferrer">Math object
reference




/>

Examples



Positive

//
value=x // x=5 5
Math.floor(value) // 5
5 5
Math.ceil(value) // 5 6 6

Math.round(value) // 5 5
6
Math.trunc(value) // 5 5 5
parseInt(value) // 5 5
5
~~value // 5 5 5
value | 0 // 5 5 5
value >> 0 //
5 5 5
value >>> 0 // 5 5 5
value - value % 1 // 5 5
5



Negative

//
value=x // x=-5 -5>x>=-5.5 -5.5>x>-6

Math.floor(value)
// -5 -6 -6
Math.ceil(value) // -5 -5 -5
Math.round(value) // -5 -5
-6
Math.trunc(value) // -5 -5 -5
parseInt(value) // -5 -5
-5
value | 0 // -5 -5 -5

~~value // -5 -5
-5
value >> 0 // -5 -5 -5
value >>> 0 // 4294967291
4294967291 4294967291
value - value % 1 // -5 -5
-5


Positive -
Larger numbers


// x =
Number.MAX_SAFE_INTEGER/10 // =900719925474099.1


//
value=x x=900719925474099 x=900719925474099.4
x=900719925474099.5

Math.floor(value) // 900719925474099
900719925474099 900719925474099
Math.ceil(value) // 900719925474099
900719925474100 900719925474100
Math.round(value) // 900719925474099
900719925474099 900719925474100
Math.trunc(value) // 900719925474099
900719925474099 900719925474099
parseInt(value) // 900719925474099
900719925474099 900719925474099
value | 0 // 858993459 858993459
858993459
~~value // 858993459 858993459 858993459
value >> 0
// 858993459 858993459 858993459

value >>> 0 // 858993459
858993459 858993459
value - value % 1 // 900719925474099 900719925474099
900719925474099


Negative
- Larger numbers


// x =
Number.MAX_SAFE_INTEGER/10 * -1 // -900719925474099.1

// value = x
// x=-900719925474099 x=-900719925474099.5
x=-900719925474099.6


Math.floor(value) //
-900719925474099 -900719925474100 -900719925474100
Math.ceil(value) //
-900719925474099 -900719925474099 -900719925474099
Math.round(value) //
-900719925474099 -900719925474099 -900719925474100
Math.trunc(value) //
-900719925474099 -900719925474099 -900719925474099
parseInt(value) //
-900719925474099 -900719925474099 -900719925474099
value | 0 // -858993459
-858993459 -858993459
~~value // -858993459 -858993459
-858993459
value >> 0 // -858993459 -858993459
-858993459
value >>> 0 // 3435973837 3435973837
3435973837
value - value % 1 // -900719925474099 -900719925474099
-900719925474099



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...