JavaScript Tutorial JavaScript References

JavaScript - Math.fround() Method



The JavaScript Math.fround() method returns the nearest 32-bit single precision float representation of the passed argument.

Syntax

Math.fround(x)

Parameters

x Specify a number. If it is not a number, it is converted to a number or to NaN if it cannot be converted.

Return Value

Returns the nearest 32-bit single precision float representation of the given number.

Example:

The example below shows the usage of Math.fround() method.

var txt;

txt = "Math.fround(2) = " + Math.fround(2) + "<br>";
txt = txt + "Math.fround(-2) = " + Math.fround(-2) + "<br>";
txt = txt + "Math.fround(1.501) = " + Math.fround(1.501) + "<br>";
txt = txt + "Math.fround(-1.501) = " + Math.fround(-1.501) + "<br>";
txt = txt + "Math.fround('1.501') = " + Math.fround('1.501') + "<br>";
txt = txt + "Math.fround('-1.501') = " + Math.fround('-1.501') + "<br>";
txt = txt + "Math.fround('2x') = " + Math.fround('2x') + "<br>";

The output (value of txt) after running above script will be:

Math.fround(2) = 2
Math.fround(-2) = -2
Math.fround(1.501) = 1.5010000467300415
Math.fround(-1.501) = -1.5010000467300415
Math.fround('1.501') = 1.5010000467300415
Math.fround('-1.501') = -1.5010000467300415
Math.fround('2x') = NaN

❮ JavaScript - Math Object