Thursday, September 4, 2025
HomeLanguagesJavascriptp5.js float() function

p5.js float() function

The float() function in p5.js is used to get the floating point representation of the given string as a parameter.

Syntax:

float(String)

Parameters: This function accepts a single parameter String which is used to be converted into its floating point representation.

Return Value: It returns the converted floating point representation.

Below programs illustrate the float() function in p5.js:

Example 1: This example uses float() function to get the floating point representation of the given string as a parameter.




function setup() { 
   
    // Creating Canvas size
    createCanvas(600, 150); 
} 
   
function draw() { 
       
    // Set the background color 
    background(220); 
     
    // Initializing some strings
    let String1 = "12";
    let String2 = "12.5";
    let String3 = "0.9";
    let String4 = "0";
     
    // Calling to float() function.
    let A = float(String1);
    let B = float(String2);
    let C = float(String3);
    let D = float(String4);
       
    // Set the size of text 
    textSize(16); 
       
    // Set the text color 
    fill(color('red')); 
     
    // Getting floating point representation
    text("Floating point representation of string '12' is: " + A, 50, 30);
    text("Floating point representation of string '12.9' is: " + B, 50, 60);
    text("Floating point representation of string '0.9' is: " + C, 50, 90);
    text("Floating point representation of string '0' is: " + D, 50, 110);
}  


Output:

Example 2: This example uses float() function to get the floating point representation of the given string as a parameter.




function setup() { 
   
    // Creating Canvas size
    createCanvas(600, 90); 
} 
   
function draw() { 
       
    // Set the background color 
    background(220); 
     
    // Initializing some strings
    let String1 = "Geeks";
    let String2 = "gfg";
     
    // Calling to float() function.
    let A = float(String1);
    let B = float(String2);
       
    // Set the size of text 
    textSize(16); 
       
    // Set the text color 
    fill(color('red')); 
     
    // Getting floating point representation
    text("Floating point representation of string 'Geeks' is: "
            + A, 50, 30);
    text("Floating point representation of string 'gfg' is: " 
            + B, 50, 60);
} 


Output:

Note: From the above example, if the parameter should be a number then it returns the output as floating point representation otherwise it returns NaN i.e, not a number.

Reference: https://p5js.org/reference/#/p5/float

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS