JavaScript Math.fround()
Syntax & Examples
Math.fround() static-method
The Math.fround() method in JavaScript returns the nearest 32-bit single-precision float representation of a given number. It is a static method of Math, meaning it is always used as Math.fround() and not as a method of a Math object created (Math is not a constructor).
Syntax of Math.fround()
The syntax of Math.fround() static-method is:
Math.fround(doubleFloat)
This fround() static-method of Math returns the nearest single precision float representation of x.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
doubleFloat | required | A number to be converted to its nearest 32-bit single-precision float representation. |
Return Type
Math.fround() returns value of type Number
.
✐ Examples
1 Using Math.fround() with a positive number
In this example, we use the Math.fround()
method to find the nearest 32-bit single-precision float representation of 1.5.
For example,
- We call
Math.fround()
with 1.5 as the argument. - The method returns the nearest 32-bit single-precision float representation of 1.5.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.fround(1.5));
Output
1.5
2 Using Math.fround() with a small number
In this example, we use the Math.fround()
method to find the nearest 32-bit single-precision float representation of 1.337.
For example,
- We call
Math.fround()
with 1.337 as the argument. - The method returns the nearest 32-bit single-precision float representation of 1.337.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.fround(1.337));
Output
1.3370000123977661
3 Using Math.fround() with a large number
In this example, we use the Math.fround()
method to find the nearest 32-bit single-precision float representation of 123456.789.
For example,
- We call
Math.fround()
with 123456.789 as the argument. - The method returns the nearest 32-bit single-precision float representation of 123456.789.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.fround(123456.789));
Output
123456.7890625
Summary
In this JavaScript tutorial, we learned about fround() static-method of Math: the syntax and few working examples with output and detailed explanation for each example.