JavaScript DataView setFloat32() Method



The JavaScript DataView setFloat32() method is used to store a 32-bit floating point number in a specific byte at the starting byte offset within this data view. You can store multiple byte values at any offset within the bounds.

This method throws a 'RangeError' exception if the value of the byteOffset parameter falls outside the bounds of this data view.

Syntax

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

setFloat32(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', as it only stores the byte value to the DataView.

Example 1

The following example demonstrates the usage of the JavaScript DataView setFloat32() method.






Output

The above program returns 'undefined' −

The byte offset: 0
Value: 23432.342
The data_view.setFloat32() method returns: undefined

Example 2

The following is another example of the JavaScript DataView setFloat32() method. We use this method to store a 32-bit floating point number 522453.23 in a byte starting at the specified byte offset 1 within this DataView.






Output

After executing the above program, it will store the floating point number within the current data view and display it as −

The byte offset: 1
Value: 522453.23
The store value is: 522453.21875

Example 3

If the value of the byteOffset parameter is -1 (outside the bounds), it will throw a 'RangeError' exception.






Output

Once the above program is executed, it will throw an exception as −

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