JavaScript Math fround()

The JavaScript Math.fround() function returns the nearest 32-bit single precision float representation of a Number.

The syntax of the Math.fround() function is:

Math.fround(doubleFloat)

fround(), being a static method, is called using the Math class name.


Math.fround() Parameters

The Math.fround() function takes in:

  • doubleFloat - A Number.

Return value from Math.fround()

  • Returns the nearest 32-bit single precision float representation of the given number.
  • Returns NaN if non-numeric argument.

Example: Using Math.fround()

var num = Math.fround(1.5);
console.log(num); // 1.5

var num = Math.fround(5.05);
console.log(num); // 5.050000190734863

console.log(2 ** 130); // 1.361129467683754e+39
var num = Math.fround(2 ** 130);
console.log(num); // Infinity

var num = Math.fround(5);
console.log(num); // 5

var num = Math.fround(1.337);
console.log(num); // 1.3370000123977661

Output

1.5
5.050000190734863
1.361129467683754e+39
Infinity
5
1.3370000123977661

JavaScript uses 64-bit double floating-point numbers internally.

Here, we can see that the numbers that can be represented perfectly in the binary numeral system (like 1.5) have the same 32-bit single precision float representation.

However, some that can't be represented perfectly (like 1.337 or 5.05) differ in 32-bit and 64-bit.

SInce 2**130 is too big for a 32-bit float, fround() returns Infinity for such numbers.


Recommended readings: