JavaScript DataView setFloat64() Method



The JavaScript DataView setFloat64() method is used to store a 64-bit floating point number in the 8 bytes (where 1 byte = 8 bits) starting at the specified byte offset within this DataView. It is possible to store multiple byte values at any byte offset within the specified bounds.

A floating point number is a positive or negative whole number with a decimal point. For example, 5.5, 0.25, -103.342, etc.

If the value of the byteOffset parameter falls outside the bounds of the data view, it will throw a 'RangeError' exception.

Syntax

Following is the syntax of the JavaScript DataView setFloat64() method −

setFloat64(byteOffset, value, littleEndian)

Parameters

This method accepts three parameters named 'byteOffset', 'value', and 'littleEndian', which are described below −

  • byteOffset − The position in the DataView where the byte will be stored.
  • value − A 32-bit floating point number that needs to be stored.
  • littleEndian − It indicates whether the data is stored in little-or-big endian format.

Return value

This method returns 'undefined'.

Example 1

The following is the basic example of the JavaScript DataView setFloat64() method.

Open Compiler
<html> <body> <script> const buffer = new ArrayBuffer(16); const data_view = new DataView(buffer); const byteOffset = 0; const value = 433.45; document.write("The byte offset: ", byteOffset); document.write("<br>Value: ", value); //using setFloat64() method document.write("<br>The setFloat64() method: ", data_view.setFloat64(byteOffset, value)); </script> </body> </html>

Output

The above program returns 'undefined' −

The byte offset: 0
Value: 433.45
The setFloat64() method: undefined

Example 2

The following is another example of the JavaScript DataViewsetFloat64()method. We use this method to store 64-bit floating point number (retrieve from) Math.PIstarting at the specified byte1within this DataView.

Open Compiler
<html> <body> <script> const buffer = new ArrayBuffer(16); const data_view = new DataView(buffer); const byteOffset = 1; const value = Math.PI; document.write("The byte offset: ", byteOffset); document.write("<br>Value: ", value); //using setFloat64() method data_view.setFloat64(byteOffset); document.write("<br>The store value: ", data_view.getFloat64(byteOffset, value)); </script> </body> </html>

Output

After executing the above program, it will store the specified floating point number within the DataView and display it as −

The byte offset: 1
Value: 3.141592653589793
The store value: 3.143e-319

Example 3

If the value of the byteOffset parameter falls outside the bounds of this data view, it will throw a 'RangeError' exception.

Open Compiler
<html> <body> <script> const buffer = new ArrayBuffer(16); const data_view = new DataView(buffer); const byteOffset = -1; const value = 16.34; document.write("The byte offset: ", byteOffset); document.write("<br>Value: ", value); try { //using setFloat64() method data_view.setFloat64(byteOffset); } catch (error) { document.write("<br>", error); } </script> </body> </html>

Output

Once the above program is executed, it will throw a 'RangeError' exception as −

The byte offset: -1
Value: 16.34
RangeError: Offset is outside the bounds of the DataView
Advertisements