JavaScript Math.fround(): Single Precision Float Examples

Deepak Prasad
Short on time?Beta Get the gist of this 2-minute read in seconds, then ask follow-up questions

JavaScript Math.fround() returns the nearest 32-bit single precision float representation of a number. JavaScript numbers are normally 64-bit double precision values; Math.fround() rounds a value as if it were stored as a 32-bit float.

Environment: Node.js v20.18.2. Each snippet is plain JavaScript; the line after it states the expected console output.


Math.fround Syntax

Conceptual stair-step showing float32 spacing when using Math.fround

javascript
Math.fround(x)
Output

The return value is the nearest 32-bit single precision float value for x.


Method 1: Round to 32-bit Float Precision

javascript
console.log(Math.fround(1.337));
console.log(Math.fround(1.5));
Output

You should see 2 lines, in order: 1.3370000123977661, 1.5.

Some numbers cannot be represented exactly as 32-bit floats, so the rounded result may look slightly different.


Method 2: Compare Math.fround(0.1) with 0.1

javascript
console.log(Math.fround(0.1) === 0.1);
Output

You should see one line logging false.

0.1 as a normal JavaScript number and 0.1 rounded to 32-bit float precision are not exactly the same value.


Method 3: Handle NaN with Math.fround()

javascript
console.log(Math.fround(NaN));
Output

You should see one line logging NaN.


Common Questions About Math.fround

What does fround mean?

fround means float round. It rounds a JavaScript number to nearest 32-bit single precision float representation.

What is the purpose of Math.fround()?

It lets JavaScript code intentionally work with float32-like precision instead of the usual 64-bit double precision number behavior.

Is Math.fround the same as Math.round?

No. Math.round() rounds to an integer. Math.fround() rounds to 32-bit floating-point precision.


Summary

Math.fround() matches float32 spacing—useful for WebGL buffers and interop, not for currency rounding. When you need two decimal places on a price label, use JavaScript toFixed() instead—it returns a formatted string, not a float32 value.

JavaScript Math.fround() returns the nearest 32-bit single precision float representation of a number. It is useful for float32-sensitive work such as graphics, binary data, and matching lower-precision numeric systems. It is not meant for ordinary decimal rounding.


Official Documentation

Finished reading?Beta Get the key takeaways in seconds, then ask follow-up questions
Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains.

  • JavaScript
  • Web Design