p5.js float() function
Last Updated :
22 Aug, 2023
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() {
createCanvas(600, 150);
}
function draw() {
background(220);
let String1 = "12" ;
let String2 = "12.5" ;
let String3 = "0.9" ;
let String4 = "0" ;
let A = float(String1);
let B = float(String2);
let C = float(String3);
let D = float(String4);
textSize(16);
fill(color( 'red' ));
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() {
createCanvas(600, 90);
}
function draw() {
background(220);
let String1 = "Geeks" ;
let String2 = "gfg" ;
let A = float(String1);
let B = float(String2);
textSize(16);
fill(color( 'red' ));
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...