dataView.setFloat64()是dataView中的內置函數,用於將帶符號的64位浮點值存儲在指定位置,即,與dataView起始位置的字節偏移處。
用法:
dataView.setFloat64(byteOffset)
參數:它具有參數byteOffset,該參數在字節中偏移,即從讀取數據的視圖開始處偏移。
返回值:此函數不返回任何內容。
JavaScript代碼顯示此方法的用法原理:
示例1:
<script>
// Creating buffer with size in byte
var buffer = new ArrayBuffer(20);
// Creating a view
var dataview1 = new DataView(buffer, 0, 10);
// put the data 56.24 at slot 1
dataview1.setFloat64(1, 56.24);
document.write(dataview1.getFloat64(1));
</script>
輸出:
56.24
示例2:
下麵的代碼使用Math.PI設置PI的值,並將輸出作為PI的值。
<script>
// Creating buffer with size in byte
var buffer = new ArrayBuffer(20);
// Creating a view with slot from o to 10
var dataview1 = new DataView(buffer, 0, 10);
// put the value of PI at slot 1
dataview1.setFloat64(1, Math.PI);
document.write(dataview1.getFloat64(1) + "<br>");
</script>
輸出:
3.141592653589793
示例3:
當沒有數據要存儲時,它將以NaN(而不是數字)形式輸出。
<script>
// Creating buffer with size in byte
var buffer = new ArrayBuffer(20);
// Creating a view
var dataview1 = new DataView(buffer, 0, 10);
// putting no data at slot 1
dataview1.setFloat64(1);
document.write(dataview1.getFloat64(1) + "<br>");
</script>
輸出:
NaN
注:本文由純淨天空篩選整理自ShivamKD大神的英文原創作品 JavaScript | dataView.setFloat64()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。