Skip to main content
Probably deserves emphasis
Source Link
Ry-
  • 226.3k
  • 56
  • 496
  • 504

If you have a stringa string, parse it as an integer:

var num = '20.536'; var result = parseInt(num, 10);  // 20 

OtherwiseIf you have a number, ECMAScript 6 offers Math.trunc for completely consistent truncation, already available in Firefox 24+ and Edge:

var num = -2147483649.536; var result = Math.trunc(num);  // -2147483649 

If you can’t rely on that and will always have a positive number, you can of course just use Math.floor:

var num = 20.536; var result = Math.floor(num);  // 20 

And finally, if you have a number in [−2147483648, 2147483647], you can truncate to 32 bits using any bitwise operator. | 0 is common, and >>> 0 can be used to obtain an unsigned 32-bit integer:

var num = -20.536; var result = num | 0;  // -20 

If you have a string, parse it as an integer:

var num = '20.536'; var result = parseInt(num, 10); // 20 

Otherwise, ECMAScript 6 offers Math.trunc for completely consistent truncation, already available in Firefox 24+ and Edge:

var num = -2147483649.536; var result = Math.trunc(num); // -2147483649 

If you can’t rely on that and will always have a positive number, you can of course just use Math.floor:

var num = 20.536; var result = Math.floor(num); // 20 

And finally, if you have a number in [−2147483648, 2147483647], you can truncate to 32 bits using any bitwise operator. | 0 is common, and >>> 0 can be used to obtain an unsigned 32-bit integer:

var num = -20.536; var result = num | 0; // -20 

If you have a string, parse it as an integer:

var num = '20.536'; var result = parseInt(num, 10);  // 20 

If you have a number, ECMAScript 6 offers Math.trunc for completely consistent truncation, already available in Firefox 24+ and Edge:

var num = -2147483649.536; var result = Math.trunc(num);  // -2147483649 

If you can’t rely on that and will always have a positive number, you can of course just use Math.floor:

var num = 20.536; var result = Math.floor(num);  // 20 

And finally, if you have a number in [−2147483648, 2147483647], you can truncate to 32 bits using any bitwise operator. | 0 is common, and >>> 0 can be used to obtain an unsigned 32-bit integer:

var num = -20.536; var result = num | 0;  // -20 
Correct compatibility
Source Link
Ry-
  • 226.3k
  • 56
  • 496
  • 504

If you have a string, parse it as an integer:

var num = '20.536'; var result = parseInt(num, 10); // 20 

Otherwise, ECMAScript 6 offers Math.trunc for completely consistent truncation, already available in Firefox 24+ and IE 11Edge:

var num = -2147483649.536; var result = Math.trunc(num); // -2147483649 

If you can’t rely on that and will always have a positive number, you can of course just use Math.floor:

var num = 20.536; var result = Math.floor(num); // 20 

And finally, if you have a number in [−2147483648, 2147483647], you can truncate to 32 bits using any bitwise operator. | 0 is common, and >>> 0 can be used to obtain an unsigned 32-bit integer:

var num = -20.536; var result = num | 0; // -20 

If you have a string, parse it as an integer:

var num = '20.536'; var result = parseInt(num, 10); // 20 

Otherwise, ECMAScript 6 offers Math.trunc for completely consistent truncation, already available in Firefox 24+ and IE 11:

var num = -2147483649.536; var result = Math.trunc(num); // -2147483649 

If you can’t rely on that and will always have a positive number, you can of course just use Math.floor:

var num = 20.536; var result = Math.floor(num); // 20 

And finally, if you have a number in [−2147483648, 2147483647], you can truncate to 32 bits using any bitwise operator. | 0 is common, and >>> 0 can be used to obtain an unsigned 32-bit integer:

var num = -20.536; var result = num | 0; // -20 

If you have a string, parse it as an integer:

var num = '20.536'; var result = parseInt(num, 10); // 20 

Otherwise, ECMAScript 6 offers Math.trunc for completely consistent truncation, already available in Firefox 24+ and Edge:

var num = -2147483649.536; var result = Math.trunc(num); // -2147483649 

If you can’t rely on that and will always have a positive number, you can of course just use Math.floor:

var num = 20.536; var result = Math.floor(num); // 20 

And finally, if you have a number in [−2147483648, 2147483647], you can truncate to 32 bits using any bitwise operator. | 0 is common, and >>> 0 can be used to obtain an unsigned 32-bit integer:

var num = -20.536; var result = num | 0; // -20 
Be specific about >>>
Source Link
Ry-
  • 226.3k
  • 56
  • 496
  • 504

If you have a string, parse it as an integer:

var num = '20.536'; var result = parseInt(num, 10); // 20 

Otherwise, ECMAScript 6 offers Math.trunc for completely consistent truncation, already available in Firefox 24+ and IE 11:

var num = -2147483649.536; var result = Math.trunc(num); // -2147483649 

If you can’t rely on that and will always have a positive number, you can of course just use Math.floor:

var num = 20.536; var result = Math.floor(num); // 20 

And finally, if you have a number in [−2147483648, 2147483647], you can truncate to 32 bits using any bitwise operator. | 0 is common, and >>> 0 can be used to obtain aan unsigned 32-bit integer:

var num = -20.536; var result = num | 0; // -20 

If you have a string, parse it as an integer:

var num = '20.536'; var result = parseInt(num, 10); // 20 

Otherwise, ECMAScript 6 offers Math.trunc for completely consistent truncation, already available in Firefox 24+ and IE 11:

var num = -2147483649.536; var result = Math.trunc(num); // -2147483649 

If you can’t rely on that and will always have a positive number, you can of course just use Math.floor:

var num = 20.536; var result = Math.floor(num); // 20 

And finally, if you have a number in [−2147483648, 2147483647], you can truncate to 32 bits using any bitwise operator. | 0 is common, and >>> 0 can be used to obtain a 32-bit integer:

var num = -20.536; var result = num | 0; // -20 

If you have a string, parse it as an integer:

var num = '20.536'; var result = parseInt(num, 10); // 20 

Otherwise, ECMAScript 6 offers Math.trunc for completely consistent truncation, already available in Firefox 24+ and IE 11:

var num = -2147483649.536; var result = Math.trunc(num); // -2147483649 

If you can’t rely on that and will always have a positive number, you can of course just use Math.floor:

var num = 20.536; var result = Math.floor(num); // 20 

And finally, if you have a number in [−2147483648, 2147483647], you can truncate to 32 bits using any bitwise operator. | 0 is common, and >>> 0 can be used to obtain an unsigned 32-bit integer:

var num = -20.536; var result = num | 0; // -20 
Reprioritize.
Source Link
Ry-
  • 226.3k
  • 56
  • 496
  • 504
Loading
Math.trunc
Source Link
Ry-
  • 226.3k
  • 56
  • 496
  • 504
Loading
How to truncate and not round for people who actually want to know a way that is fast, readable and non-horrible
Source Link
Ry-
  • 226.3k
  • 56
  • 496
  • 504
Loading
deleted 1 characters in body
Source Link
Ry-
  • 226.3k
  • 56
  • 496
  • 504
Loading
Source Link
Ry-
  • 226.3k
  • 56
  • 496
  • 504
Loading