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.
<html>
<body>
<script>
const buffer = new ArrayBuffer(16);
const data_view = new DataView(buffer);
const byteOffset = 0;
const value = 23432.342;
document.write("The byte offset: ", byteOffset);
document.write("<br>Value: ", value);
document.write("<br>The data_view.setFloat32() method returns: ", data_view.setFloat32(byteOffset, value));
</script>
</body>
</html>
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.
<html>
<body>
<script>
const buffer = new ArrayBuffer(16);
const data_view = new DataView(buffer);
const byteOffset = 1;
const value = 522453.23;
document.write("The byte offset: ", byteOffset);
document.write("<br>Value: ", value);
//using setFloat32() method
data_view.setFloat32(byteOffset, value)
document.write("<br>The store value is: ", data_view.getFloat32(byteOffset));
</script>
</body>
</html>
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.
<html>
<body>
<script>
const buffer = new ArrayBuffer(16);
const data_view = new DataView(buffer);
const byteOffset = -1;
const value = 23322.422;
document.write("The byte offset: ", byteOffset);
document.write("<br>Value: ", value);
try {
//using setFloat32() method
data_view.setFloat32(byteOffset, value)
} catch (error) {
document.write("<br>", error);
}
</script>
</body>
</html>
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