setFloat64() writes a number into eight bytes of a DataView as a 64-bit floating point value (double precision). Since every JavaScript Number is already stored this way, nothing is rounded: the exact bit pattern goes into the buffer. That makes setFloat64() the safe choice for saving measurements, calculation results or any value you must read back identically. Pass true as the third argument for little-endian order.
Converts value to a 64-bit floating point number (double precision) and writes it into the 8 bytes starting at byteOffset, big-endian. Returns undefined.
view.setFloat64(byteOffset, value, true)
Writes the same 8 bytes in little-endian order, so the least significant byte lands at byteOffset.
Part of the original DataView from ES2015, so every modern browser and runtime has it. Unlike a Float64Array, a DataView has no alignment rule: setFloat64() works at any byte position, even one that is not a multiple of 8.
Library declarations
JavaScript
DataView.prototype.setFloat64(byteOffset, value)
DataView.prototype.setFloat64(byteOffset, value, littleEndian)
// returns undefined; writes 8 bytes starting at byteOffset
// value is converted to a 64-bit floating point number
// needs byteOffset + 8 <= view.byteLength, else RangeError
// littleEndian: false or left out = big-endian
Parameters
Parameter
Description
byteOffset
Where to start writing, 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 8 bytes must fit: byteOffset + 8 may not be larger than view.byteLength. A negative or too-large offset throws a RangeError.
value
The number to store. Every JavaScript Number is already a 64-bit float, so it is stored exactly, including NaN, Infinity and -0.
littleEndian
Optional. Leave it out (or pass false) for big-endian: the most significant of the 8 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 Value
Description
undefined
setFloat64() gives nothing back. Its result is the 8 bytes it changed inside the buffer; read them back with getFloat64().
JavaScript Examples
Example 1 — Compare the Bytes of 0.1 + 0.2 and 0.3
Write both numbers as doubles and compare their raw bytes to see the famous difference.
Two numbers that look equal. In JavaScript 0.1 + 0.2 is 0.30000000000000004, not exactly 0.3; both are written as doubles.
Compare the raw bytes. The two 8-byte patterns are identical except for the very last bit: 34 versus 33.
The difference is real. Reading both back with getFloat64() and comparing them gives false, which is why decimals should be compared with a small tolerance.
Example 2 — Save the Speed of Light Little-Endian
Write a physical constant with true, then read it back in both byte orders.
JavaScript
const c = 299792458; // metres per second
const view = new DataView(new ArrayBuffer(8));
view.setFloat64(0, c, true);
console.log(view.getFloat64(0, true) === c); // true
console.log(view.getFloat64(0) === c); // false
console.log(view.getUint8(7).toString(16)); // 41