Categories
JavaScript Web development

JavaScript Math.fround()

JavaScript Math.fround()

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

Syntax:

Math.fround(x)

Example:

<!DOCTYPE html>
<html>
<body>

<button onclick="froundFunction()">Try it</button>

<p id="example"></p>

<script>
function froundFunction() {
  var a = Math.fround(3.70);
  var b = Math.fround(3.50);
  var c = Math.fround(3.49);
  var d = Math.fround(-3.60);
 
  var x = a + "<br>" + b + "<br>" + c + "<br>" + d;
  document.getElementById("example").innerHTML = x; 
}
</script>

</body>
</html>

Output:

Round different numbers to the nearest (32-bit single precision) float representation of a number:


Enjoy coding!

Read also:

JavaScript Math Object

JavaScript Math Object Properties

JavaScript Math.cbrt()