p5.js中的float()函數用於獲取給定字符串的浮點表示形式作為參數。
用法:
float(String)
參數:該函數接受單個參數String,該參數用於轉換為其浮點表示形式。
返回值:它返回轉換後的浮點表示形式。
以下程序說明了p5.js中的float()函數:
示例1:本示例使用float()函數獲取給定字符串的浮點表示形式作為參數。
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);
}
輸出:
示例2:本示例使用float()函數獲取給定字符串的浮點表示形式作為參數。
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);
}
輸出:
注意:在上麵的示例中,如果參數應為數字,則它將以浮點表示形式返回輸出,否則將返回NaN,即不是數字。
參考: https://p5js.org/reference/#/p5/float
相關用法
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 p5.js | float() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。