p5.js | nf() Function
The nf() function in p5.js is used to format the input numbers (integers or floats) into strings.
Syntax:
nf(Num/Arr, Left, Right)
Parameters: This function accepts three parameters as mentioned above and described below:
- Num/Arr: This parameter holds the input positive number or an array of numbers which are to be formatted.
- Left: This is the positive number which says the number of digits should be on the left side of the decimal point.
- Right: This is the positive number which says the number of digits should be on the right side of the decimal point.
Return Value: It returns the formatted string.
Below programs illustrate the nf() function in p5.js:
Example 1: This example uses nf() function to format the input numbers into strings.
function setup() { // Creating Canvas size createCanvas(450, 200); } function draw() { // Set the background color background(220); // Initializing the Numbers let num1 = 345; let num2 = 12.3; let num3 = .5; let num4 = .05; let num5 = 0; let num6 = 0.7; // Calling to nf() function. let A = nf(num1, 4, 3); let B = nf(num2, 4, 2); let C = nf(num3, 5, 3); let D = nf(num4, 2, 3); let E = nf(num5, 2, 2); let F = nf(num6, 4, 3); // Set the size of text textSize(16); // Set the text color fill(color('red')); // Getting formatted String text("Formatted String is: " + A, 50, 30); text("Formatted String is: " + B, 50, 60); text("Formatted String is: " + C, 50, 90); text("Formatted String is: " + D, 50, 110); text("Formatted String is: " + E, 50, 140); text("Formatted String is: " + F, 50, 170); } |
Output:

Example 2: This example uses nf() function to format the input numbers into strings.
function setup() { // Creating Canvas size createCanvas(450, 90); } function draw() { // Set the background color background(220); // Initializing the array of numbers let num1 = [345, 0, 2]; let num2 = [12.3, .4, 2.0]; // Calling to nf() function. let A = nf(num1, 4, 3); let B = nf(num2, 4, 2); // Set the size of text textSize(16); // Set the text color fill(color('red')); // Getting formatted String text("Formatted String is: " + A, 50, 30); text("Formatted String is: " + B, 50, 60); } |
Output:

Reference: https://p5js.org/reference/#/p5/nf
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- D3.js | d3.map.has() Function
- D3.js | d3.map.get() Function
- p5.js | value() Function
- p5.js | red() function
- p5.js | max() function
- p5.js | min() function
- p5.js | hue() function
- CSS | rgb() Function
- PHP | Ds\Set contains() Function
- D3.js | d3.set.add() Function
- D3.js | d3.hcl() Function
- p5.js | cos() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


