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 );

Math object reference


Examples

Positive
// value=x        //  x=5          5<x<5.5      5.5<=x<6  

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
Answer from moonshadow on Stack Overflow
Top answer
1 of 16
2261
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 );

Math object reference


Examples

Positive
// value=x        //  x=5          5<x<5.5      5.5<=x<6  

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
2 of 16
348

Bitwise OR operator

A bitwise or operator can be used to truncate floating point figures and it works for positives as well as negatives:

function float2int (value) {
    return value | 0;
}

Results

float2int(3.1) == 3
float2int(-3.1) == -3
float2int(3.9) == 3
float2int(-3.9) == -3

Performance comparison?

I've created a JSPerf test that compares performance between:

  • Math.floor(val)
  • val | 0 bitwise OR
  • ~~val bitwise NOT
  • parseInt(val)

that only works with positive numbers. In this case you're safe to use bitwise operations well as Math.floor function.

But if you need your code to work with positives as well as negatives, then a bitwise operation is the fastest (OR being the preferred one). This other JSPerf test compares the same where it's pretty obvious that because of the additional sign checking Math is now the slowest of the four.

Note

As stated in comments, BITWISE operators operate on signed 32bit integers, therefore large numbers will be converted, example:

1234567890  | 0 => 1234567890
12345678901 | 0 => -539222987
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseInt
parseInt() - JavaScript - MDN Web Docs
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-a-float-number-to-the-whole-number-in-javascript
How to Convert a Float Number to the Whole Number in JavaScript? - GeeksforGeeks
July 11, 2025 - Below are various methods to convert float numbers to whole numbers in JavaScript: ... Round off the number passed as a parameter to its nearest integer in the Downward direction.
🌐
W3Schools
w3schools.com › js › js_number_methods.asp
JavaScript Number Methods
Safe integers are all integers from -(253 - 1) to +(253 - 1). This is safe: 9007199254740991. This is not safe: 9007199254740992. For a complete reference to all JavaScript properties and methods, with full descriptions and many examples, go to:
🌐
Educative
educative.io › answers › how-to-convert-string-to-int-or-number-in-javascript
How to convert string to int or number in JavaScript
In JavaScript, the double tilde (~~) operator can be used to convert a string to a number, but this method will truncate any decimal portions if the input string contains a decimal value.
🌐
W3Schools
w3schools.com › jsref › jsref_parseint.asp
JavaScript parseInt() Method
The parseInt method parses a value as a string and returns the first integer. A radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal. If radix is omitted, JavaScript assumes radix 10.
🌐
TutorialsPoint
tutorialspoint.com › how-do-i-convert-a-float-number-to-a-whole-number-in-javascript
How do I convert a float number to a whole number in JavaScript?
July 20, 2022 - In the above output users can see that Math.trunc() method has removed the decimal part and Math.round() method has rounded the number to nearest integer. In this approach, we will use the parseInt() method. The parseInt() method extracts the whole number from the float number or string in ...
🌐
UsefulAngle
usefulangle.com › post › 238 › javascript-get-integer-from-decimal-float
Get Integer Value from a Decimal Number in Javascript
December 19, 2019 - The number can also be rounded to the next integer using Math.round(), Math.ceil() and Math.floor() methods. The Math.trunc() method simply truncates the decimal part of the number and returns the integer part.
🌐
Byby
byby.dev › js-float-to-int
How to convert float to integer in JavaScript
Converting a float to an integer is a common operation that allows you to work with whole numbers instead of decimal values. You can use various methods that either round, truncate, or parse the float value. Depending on your specific use case, you can choose the most appropriate method: Using Math.trunc(). This method returns the integer part of a number by removing any fractional digits.
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript float to int
How to Convert a Float Number to Int in JavaScript | Delft Stack
February 2, 2024 - If we are interested in processing the float number to round it or get the nearest integer based on the business requirement, it would be good to go for Number.toFixed(), Math.round(), Math.ceil() and the Math.floor() functions.
🌐
W3docs
w3docs.com › javascript
How to Convert a Float Number to Whole Number in JavaScript
The parseInt() function parses the string argument and returns an integer of the given radix. ... The ~~ operator rounds a number towards 0 if the operand is a number and it’s not NaN or Infinity. ... The OR operator rounds a number towards 0. ... There are two types of numbers in JavaScript: Regular and BigInt.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number
Number - JavaScript - MDN Web Docs - Mozilla
255; // two-hundred and fifty-five 255.0; // same number 255 === 255.0; // true 255 === 0xff; // true (hexadecimal notation) 255 === 0b11111111; // true (binary notation) 255 === 0.255e3; // true (decimal exponential notation) A number literal like 37 in JavaScript code is a floating-point value, not an integer. There is no separate integer type in common everyday use. (JavaScript also has a BigInt type, but it's not designed to replace Number for everyday uses.
🌐
Mikemcl
mikemcl.github.io › decimal.js
decimal.js API
number: integer, -9e15 to 0 inclusive Default value: -9e15 · The negative exponent limit, i.e. the exponent value below which underflow to zero occurs. If the Decimal to be returned by a calculation would have an exponent lower than minE then the value of that Decimal becomes zero. JavaScript numbers underflow to zero for exponents below -324.
🌐
Quora
quora.com › Given-a-number-with-decimals-in-JavaScript-how-do-I-unpack-its-decimals-and-its-integers-into-two-different-variables
Given a number with decimals in JavaScript, how do I unpack its decimals and its integers into two different variables? - Quora
Answer (1 of 9): Think about this a minute - That’s the problem w/ Quora programming question askers lol - Not thinking about the questions prior to asking. Let’s say you have a number 6.23. Now you want to break that apart if I understood you right into 2 variables, one with 6 and the ...
Top answer
1 of 2
1

In JavaScript you cant define variable with zeros after dot 1.00 Javascript engine clear all zeros in the end of numbers after dot.

examples:

let oneZero = 1.0;
console.log('oneZero:',oneZero);
let twoZeros = 1.00;
console.log('twoZeros:',twoZeros);
let oneAndTen = 1.10;
console.log('1.10:',oneAndTen);
let threeZeros = 1.000;
console.log('threeZeros:',threeZeros)

For fix your problem you need just format like a string your number.

let formatedString = 1+".00";

But it's not best solution. You need use some like numeraljs library

2 of 2
0

In javascript you can try with using Number() function to convert a string to a number (integer or decimal) if needed, and you can use parseFloat(number) method.

Examples on using Number():

// Convert strings
Number('123'); // returns 123
Number('12.3'); // returns 12.3
Number('3.14someRandomStuff'); // returns NaN
Number('42px'); // returns NaN

Examples on using parseFloat():

var text = '3.14someRandomStuff';
var pointNum = parseFloat(text);
// returns 3.14

In your particular case however, this is how it could work as an example: (Edited, updated code, using the OP's code, since I had a little more time now to clear this for later, useful reference)

<?php
$total_count = (string) "£3,400.00";
$total_commission = (string) "£3,500.25";
$active_refs = (string) "£4,400.87";
?>

<div class="container2">
  <div id="value1"></div>
  <div id="value2"></div>
  <div id="value3"></div>
</div>

<script>  

// getting a float number from a formatted PHP string
var getNum = function (str) {
    var num = str.replace(/[\£\,]/mg, '');
    var num2 = parseFloat(num);
    return num2;
}

// converting the counter value back to a currency format
var convertNum = function(number){
    let numb = number;
    return numb.toLocaleString('en-GB', {style : 'currency', currency : 'GBP', minimumFractionDigits : '2', currencyDisplay : 'symbol'});
}

var value1 = getNum("<?php echo (string) $total_count ?>");
var value2 = getNum("<?php echo (string) $total_commission ?>");
var value3 = getNum("<?php echo (string) $active_refs ?>");

// Count Up
function animateValue(id, start, end, duration) {
    var obj = document.getElementById(id);
    var range = end - start;
    var minTimer = 50;
    var stepTime = Math.abs(Math.floor(duration / range));
    // never go below minTimer
    stepTime = Math.max(stepTime, minTimer);
    // get current time and calculate desired end time
    var startTime = new Date().getTime();
    var endTime = startTime + duration;
    var timer;

  function run() {
      var now = new Date().getTime();
      var remaining = Math.max((endTime - now) / duration, 0);
      var value = parseFloat(end - (remaining * range));
      obj.innerHTML = convertNum(value);
      if (value == end) {
          clearInterval(timer);
      }
  }

  timer = setInterval(run, stepTime);
  run();

}

animateValue("value1", 1, value1, 1750);
animateValue("value2", 1, value2, 1750);
animateValue("value3", 1, value3, 1750);

</script>

And you can also check the result/output of the above code version here: or if it was deleted already, then here

And maybe less importantly I’d like to point out that in your code the following line converted the decimal to integer which had to be edited too:

var value = Math.round(end - (remaining * range));

This one line would return back an integer number.

🌐
Rip Tutorial
riptutorial.com › float to integer
JavaScript Tutorial => Float to Integer
To convert a float to an integer, JavaScript provides multiple methods. The floor function returns the first integer less than or equal to the float. ... The ceil function returns the first integer greater than or equal to the float. ... The round function rounds the float. ... Truncation (trunc) removes the decimals from the float.
🌐
Programiz
programiz.com › javascript › examples › decimal-binary
JavaScript Program to Convert Decimal to Binary
// program to convert decimal to ... ' ' + result); ... In the above program, the user is prompted to enter a number. The parseInt() method is used to convert a string value to an integer. The JavaScript built-in method toString([radix]) returns a string value in a specified ...
🌐
Codemia
codemia.io › knowledge-hub › path › how_do_i_convert_a_float_number_to_a_whole_number_in_javascript
How do I convert a float number to a whole ...
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises