📜  p5.js |布尔()函数

📅  最后修改于: 2022-05-13 01:56:56.276000             🧑  作者: Mango

p5.js |布尔()函数

p5.js 中的boolean()函数用于将给定的字符串和数值转换为其布尔表示形式。

句法:

boolean(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