JavaScript DataView getFloat64() 方法用于检索从此 DataView 中的指定字节偏移量开始的 8 字节浮点数。可以在指定边界内的任何字节偏移量处检索多个字节值。
浮点数是带小数点的正整数或负整数。例如,5.5、0.25、-103.342 等。
如果 byteOffset 参数超出数据视图的边界,它将引发 'RangeError' 异常。
语法
以下是 JavaScript DataView getFloat64() 方法的语法 -
getFloat64(byteOffset, littleEndian)
参数
此方法接受两个名为 'byteOffset' 和 'littleEndian' 的参数,如下所述 -
- byteOffset - DataView 中要从中读取数据的位置。
- littleEndian − 它表示数据是以 little-endian 还是 big endian 格式存储。
返回值
此方法返回一个数字值。
示例 1
以下程序演示了 JavaScript DataView getFloat64() 方法的用法。
<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>
输出
上面的程序将 store 值返回为 −
The byte offset: 0
Value: 433.45
The getFloat64() method: 4.667261456827042e-62
Value: 433.45
The getFloat64() method: 4.667261456827042e-62
示例 2
以下是 JavaScript DataView getFloat64() 方法的另一个示例。我们使用此方法检索从此 DataView 中的指定字节偏移量 1 开始的 64 位浮点数。
<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>
输出
执行上述程序后,它会将指定的浮点数存储在数据视图中。
The byte offset: 1
Value: 3.141592653589793
The store value: 3.207375630676366e-192
Value: 3.141592653589793
The store value: 3.207375630676366e-192
示例 3
如果 byteOffset 参数的值超出此数据视图的边界,它将引发 'RangeError' 异常。
<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>
输出
一旦执行了上述程序,它将抛出一个 'RangeError' 异常,即 -
The byte offset: -1
The value: 16.34
Error: RangeError: Offset is outside the bounds of the DataView
The value: 16.34
Error: RangeError: Offset is outside the bounds of the DataView