JavaScript DataView getFloat32() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Instance method

Definition

getFloat32() reads four bytes from a DataView as a 32-bit floating point number (single precision) and returns it as a normal JavaScript Number. A 32-bit float keeps about 7 significant digits and reaches about ±3.4 × 1038, which is plenty for sensor values, audio and 3D coordinates. Because JavaScript Numbers have more precision, values like 0.1 show their rounding when read back.

Remember
const view = new DataView(new Uint8Array([0x3f, 0x80, 0x00, 0x00]).buffer);
view.getFloat32(0);         // 1
view.getFloat32(0, true);   // 4.600602988224807e-41
view.getUint32(0);          // 1065353216
view.getFloat32(1);         // RangeError

Syntax

Call getFloat32() on a DataView object with a byte position and, if the data is little-endian, true.

JavaScript
dataview.getFloat32(byteOffset)
dataview.getFloat32(byteOffset, littleEndian)
CallWhat it does
view.getFloat32(byteOffset)Reads the 4 bytes starting at byteOffset in big-endian order (most significant byte first) and returns a 32-bit floating point number (single precision).
view.getFloat32(byteOffset, true)Reads the same 4 bytes in little-endian order (least significant byte first), the order used by most file formats from PCs, such as WAV and BMP.

Part of the original DataView from ES2015, so every modern browser and runtime has it. Unlike a Float32Array, a DataView has no alignment rule: getFloat32() works at any byte position, even one that is not a multiple of 4.

Library declarations
JavaScript
DataView.prototype.getFloat32(byteOffset)
DataView.prototype.getFloat32(byteOffset, littleEndian)

// returns a Number: about -3.4028234663852886e38 to 3.4028234663852886e38, about 7 significant digits
// reads 4 bytes starting at byteOffset (counted from the start of the view)
// needs byteOffset + 4 <= view.byteLength, else RangeError
// littleEndian: false or left out = big-endian

Parameters

ParameterDescription
byteOffsetWhere to start reading, counted in bytes from the start of the view (not of the underlying buffer). It is converted to a whole number, so 1.9 means 1 and "2" means 2. All 4 bytes must fit: byteOffset + 4 may not be larger than view.byteLength. A negative or too-large offset throws a RangeError.
littleEndianOptional. Leave it out (or pass false) for big-endian: the most significant of the 4 bytes comes first. Pass true for little-endian: the least significant byte comes first. Any truthy value, such as 1, also means little-endian.

Return Value

Return ValueDescription
NumberThe 4 bytes decoded as a 32-bit floating point number (single precision) and turned into a normal JavaScript Number (range about -3.4028234663852886e38 to 3.4028234663852886e38, about 7 significant digits). It can also be NaN, Infinity, -Infinity or -0.

JavaScript Examples

Example 1 — Read a Packet With Type, Length and a Float Value

A sensor sends 7 bytes: a 1-byte type, a 2-byte length and a 4-byte float temperature, packed with no gaps.

JavaScript
const packet = new Uint8Array([0x01, 0x00, 0x04, 0x41, 0xac, 0x00, 0x00]);
const view = new DataView(packet.buffer);

const type = view.getUint8(0);
const length = view.getUint16(1);
const value = view.getFloat32(3);

console.log("type " + type + ", " + length + " bytes");   // type 1, 4 bytes
console.log("temperature: " + value + " C");              // temperature: 21.5 C
Try It Yourself

How It Works

  1. Three fields in 7 bytes. The type, the length and the value sit right after each other, which is typical for small binary protocols.
  2. Read each field at its offset. getUint8(0) gives the type, getUint16(1) the length, and getFloat32(3) reads the 4 bytes starting at offset 3.
  3. The float needs no alignment. Offset 3 is not a multiple of 4, which a Float32Array would reject; DataView reads 21.5 without trouble.

Example 2 — Read Back 0.1 From a 32-bit Float

Store 0.1 as a 32-bit float and compare what comes back with the original Number.

JavaScript
const view = new DataView(new ArrayBuffer(4));
view.setFloat32(0, 0.1);

const back = view.getFloat32(0);
console.log(back);                        // 0.10000000149011612
console.log(back === 0.1);                // false
console.log(back.toFixed(2));             // 0.10
console.log(back === Math.fround(0.1));   // true
Try It Yourself

How It Works

  1. Store 0.1 in 4 bytes. setFloat32() must round 0.1 to the nearest 32-bit float, because 0.1 has no exact binary form.
  2. Read it back and look closely. getFloat32() returns that rounded value as a 64-bit Number, so the extra digits 0.10000000149011612 become visible and === 0.1 is false.
  3. Display and compare sensibly. toFixed(2) prints 0.10 for people, and Math.fround(0.1) produces the same float32 value for an exact comparison.

Example 3 — A Little-Endian Float From a Device

A device sends the float -12.5 low byte first. Read it with and without true.

JavaScript
const bytes = new Uint8Array([0x00, 0x00, 0x48, 0xc1]);
const view = new DataView(bytes.buffer);

console.log(view.getFloat32(0, true));     // -12.5
const wrong = view.getFloat32(0);
console.log(wrong > 0 && wrong < 1e-30);   // true
Try It Yourself

How It Works

  1. Bytes from a little-endian device. The 4 bytes 00 00 48 c1 are the float -12.5 (0xc1480000) stored low byte first.
  2. Ask for little-endian. getFloat32(0, true) puts the bytes back in the right order and gives -12.5.
  3. The wrong order looks like almost zero. Read big-endian, the same bytes form a tiny positive number around 1e-41: a typical sign of a byte-order mistake.

Browser & Runtime Support

getFloat32() has been part of JavaScript since ES2015, when DataView itself was standardised.

✓ ES2015 · Baseline

DataView.prototype.getFloat32()

Works in every modern browser and in Node.js, Deno and Bun. It runs on a normal ArrayBuffer, so no special page setup is needed.

100% Modern browser support
Google Chrome Modern versions · Desktop & Mobile
Full support
Mozilla Firefox Modern versions · Desktop & Mobile
Full support
Apple Safari Modern versions · macOS & iOS
Full support
Microsoft Edge Modern versions · Chromium
Full support
Opera Modern versions
Full support
Samsung Internet Modern versions · Android
Full support
Internet Explorer Retired browser · not tested here
Legacy
Node.js Current releases · checked on Node 24
Supported
Deno Current releases
Supported
Bun Current releases
Supported
getFloat32() All modern engines

Bottom line: Safe to use without a fallback. It also works on a SharedArrayBuffer, but a normal ArrayBuffer is all you need.

Frequently Asked Questions

About 7 significant decimal digits. Integers are exact only up to 16777216; above that, some whole numbers are skipped.
The bytes hold the nearest 32-bit float to 0.1. getFloat32() converts it to a 64-bit Number, which shows the small error that the 32-bit format had.
It returns Infinity, -Infinity or NaN, exactly as the 32-bit float pattern says.
Yes. DataView has no alignment rule, which makes it ideal for packed binary packets.
Use the one that matches how the data was written. If the format stores 4-byte floats, use getFloat32(); if it stores 8-byte doubles, use getFloat64().

Next: getFloat64()

Read an 8-byte float: the same format as every JavaScript Number.

getFloat64() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful