p5.js中的boolean()函數用於將給定的字符串和數字值轉換為其布爾表示形式。
用法:
boolean(Value)
參數:此函數接受單個參數Value,該參數將轉換為其布爾表示形式。該值可以是整數,浮點數,字符串,布爾值,負值或正值以及值數組。
返回值:它返回轉換後的布爾表示形式。
以下示例程序旨在說明p5.js中的boolean()函數:
示例1:本示例使用boolean()函數將輸入值轉換為其布爾值。
function setup() {
// Creating Canvas size
createCanvas(600, 200);
}
function draw() {
// Set the background color
background(220);
// Initializing some values
let Value1 = 12;
let Value2 = 12.5;
let Value3 = -7.9;
let Value4 = -6;
let Value5 = "6";
let Value6 = 0;
// Calling to boolean() function.
let A = boolean(Value1);
let B = boolean(Value2);
let C = boolean(Value3);
let D = boolean(Value4);
let E = boolean(Value5);
let F = boolean(Value6);
// Set the size of text
textSize(16);
// Set the text color
fill(color('red'));
// Getting boolean representation
text("Boolean representation of value 12 is: " + A, 50, 30);
text("Boolean representation of value 12.5 is: " + B, 50, 60);
text("Boolean representation of value -7.9 is: " + C, 50, 90);
text("Boolean representation of value -6 is: " + D, 50, 110);
text("Boolean representation of string '6' is: " + E, 50, 140);
text("Boolean representation of string 0 is: " + F, 50, 170);
}
輸出:
示例2:本示例使用boolean()函數將輸入值轉換為其布爾值。
function setup() {
// Creating Canvas size
createCanvas(600, 140);
}
function draw() {
// Set the background color
background(220);
// Initializing some values
let Value1 = true;
let Value2 = false;
let Value3 = "Geeks";
let Value4 = [12, 3.6, -9.8, "true", false, "Geeks"];
// Calling to boolean() function.
let A = boolean(Value1);
let B = boolean(Value2);
let C = boolean(Value3);
let D = boolean(Value4);
// Set the size of text
textSize(16);
// Set the text color
fill(color('red'));
// Getting boolean representation
text("Boolean representation of value 'true' is: " + A, 50, 30);
text("Boolean representation of value 'false' is: " + B, 50, 60);
text("Boolean representation of value 'Geeks' is: " + C, 50, 90);
text("Boolean representation of array of values are: " + D, 50, 110);
}
輸出:
注意:在上麵的示例中,如果參數為非零值,則返回true;對於字符串,僅“true”字符串返回true;否則,對於其他任何字符串,返回false。
參考: https://p5js.org/reference/#/p5/boolean
相關用法
- Javascript boolean.toString()用法及代碼示例
- Javascript boolean.valueOf()用法及代碼示例
- PHP string轉boolean用法及代碼示例
- javascript Boolean constructor用法及代碼示例
- Javascript string轉boolean用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 p5.js | boolean() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。