Open In App

Node.js Buffer.readFloatLE() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The buf.readFloatLE() method is used to read a 32 bit float number from Buffer class with specified endian format. It facilitate interaction among octet streams inside TCP streams, operations of the file system and various other factors.

Syntax:

buffer.readFloatLE( integer )

Parameters: This method accepts single parameter offset which holds the value which tells about how many numbers of bytes to skip before initializing the read operation. The value of offset lies between 0 to buffer.length – 4. The default value is 0.

Return Value: It returns an integer value in little endian format.

Below examples illustrate the use of buf.readFloatLE() Method in Node.js:

Example 1:




// Node.js program to demonstrate the  
// buffer.readFloatLE() Method
      
// Creating a buffer of given size 
const buf = Buffer.from([5, 6, 7, 8]); 
      
// Display the result 
console.log(buf.readFloatLE(0)); 
  
console.log(buf); 


Output:

4.063216068939723e-34
<Buffer 05 06 07 08>

Example 2:




// Node.js program to demonstrate the  
// buffer.readFloatLE() Method
      
// Creating a buffer of given size 
const buf = Buffer.from([55, 66, 77, 88]); 
      
// Display the result 
console.log(buf.readFloatLE(0)); 
  
console.log(buf); 


Output:

internal/buffer.js:72
  throw new ERR_OUT_OF_RANGE(type || 'offset',
  ^

RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range.
 It must be >= 0 and <= 0. Received 2
. . . 

Note: The above program will compile and run by using the node index.js command.

Reference: https://nodejs.org/api/buffer.html#buffer_buf_readfloatle_offset


Last Updated : 13 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads