JavaScript DataView getFloat64() Method



The JavaScript DataViewgetFloat64()method is used to retrieve an 8-byte floating point number starting at the specified byte offset within this DataView. It is possible to retrievemultiple bytevalues 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 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 getFloat64() method −

getFloat64(byteOffset, littleEndian)

Parameters

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

  • byteOffset − The position in the DataView from which to read the data.
  • littleEndian − It indicates whether the data is stored in little-or-big endian format.

Return value

This method returns a number value.

Example 1

The following program demonstrates the usage of the JavaScript DataView getFloat64() method.

<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);
   //set the value
   data_view.setFloat64(byteOffset, value);
   //using getFloat64() method
   document.write("<br>The getFloat64() method: ", data_view.getFloat64(byteOffset, value));
</script>
</body>
</html>

Output

The above program returns the store value as −

The byte offset: 0
Value: 433.45
The getFloat64() method: 4.667261456827042e-62

Example 2

The following is another example of the JavaScript DataView getFloat64() method. We use this method to retrieve a 64-bit floating point number starting at the specified byte offset 1 within this DataView.

<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);
   //set the value
   data_view.setFloat64(byteOffset, value);
   //using getFloat64() method
   data_view.getFloat64(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 data view.

The byte offset: 1
Value: 3.141592653589793
The store value: 3.207375630676366e-192

Example 3

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

<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>The value: ", value);
   try {
      data_view.setFloat64(byteOffset, value);
      //using getFloat64() method
      document.write(data_view.getFloat64(byteOffset));
   } catch (error) {
      document.write("<br>Error: ", error);
   }
</script>
</body>
</html>

Output

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

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