Skip to content

Commit ac73a86

Browse files
authored
Merge pull request #3314 from vipulrawat/doc-additions-vipul
Improved docs and examples of nfs() and nf()
2 parents 6b34eb4 + f474159 commit ac73a86

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

src/utilities/string_functions.js

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ p5.prototype.matchAll = function(str, reg) {
134134
* versions: one for formatting floats, and one for formatting ints.
135135
* The values for the digits, left, and right parameters should always
136136
* be positive integers.
137+
* (NOTE): Be cautious when using left and right parameters as it prepends numbers of 0's if the parameter
138+
* if greater than the current length of the number.
139+
* For example if number is 123.2 and left parameter passed is 4 which is greater than length of 123
140+
* (integer part) i.e 3 than result will be 0123.2. Same case for right parameter i.e. if right is 3 than
141+
* the result will be 123.200.
137142
*
138143
* @method nf
139144
* @param {Number|String} num the Number to format
@@ -146,30 +151,31 @@ p5.prototype.matchAll = function(str, reg) {
146151
* @example
147152
* <div>
148153
* <code>
154+
* var myFont;
155+
* function preload() {
156+
* myFont = loadFont('assets/fonts/inconsolata.ttf');
157+
* }
149158
* function setup() {
150159
* background(200);
151-
* var num = 112.53106115;
160+
* var num1 = 321;
161+
* var num2 = -1321;
152162
*
153163
* noStroke();
154164
* fill(0);
155-
* textSize(14);
156-
* // Draw formatted numbers
157-
* text(nf(num, 5, 2), 10, 20);
158-
*
159-
* text(nf(num, 4, 3), 10, 55);
160-
*
161-
* text(nf(num, 3, 6), 10, 85);
165+
* textFont(myFont);
166+
* textSize(22);
162167
*
163-
* // Draw dividing lines
168+
* text(nf(num1, 4, 2), 10, 30);
169+
* text(nf(num2, 4, 2), 10, 80);
170+
* // Draw dividing line
164171
* stroke(120);
165-
* line(0, 30, width, 30);
166-
* line(0, 65, width, 65);
172+
* line(0, 50, width, 50);
167173
* }
168174
* </code>
169175
* </div>
170176
*
171177
* @alt
172-
* "0011253" top left, "0112.531" mid left, "112.531061" bottom left canvas
178+
* "0321.00" middle top, -1321.00" middle bottom canvas
173179
*/
174180
/**
175181
* @method nf
@@ -374,10 +380,18 @@ function addNfp(num) {
374380

375381
/**
376382
* Utility function for formatting numbers into strings. Similar to <a href="#/p5/nf">nf()</a> but
377-
* puts a " " (space) in front of positive numbers and a "-" in front of
378-
* negative numbers. There are two versions: one for formatting floats, and
379-
* one for formatting ints. The values for the digits, left, and right
380-
* parameters should always be positive integers.
383+
* puts an additional "_" (space) in front of positive numbers just in case to align it with negative
384+
* numbers which includes "-" (minus) sign.
385+
* The main usecase of nfs() can be seen when one wants to align the digits (place values) of a positive
386+
* number with some negative number (See the example to get a clear picture).
387+
* There are two versions: one for formatting float, and one for formatting int.
388+
* The values for the digits, left, and right parameters should always be positive integers.
389+
* (IMP): The result on the canvas basically the expected alignment can vary based on the typeface you are using.
390+
* (NOTE): Be cautious when using left and right parameters as it prepends numbers of 0's if the parameter
391+
* if greater than the current length of the number.
392+
* For example if number is 123.2 and left parameter passed is 4 which is greater than length of 123
393+
* (integer part) i.e 3 than result will be 0123.2. Same case for right parameter i.e. if right is 3 than
394+
* the result will be 123.200.
381395
*
382396
* @method nfs
383397
* @param {Number} num the Number to format
@@ -390,19 +404,27 @@ function addNfp(num) {
390404
* @example
391405
* <div>
392406
* <code>
407+
* var myFont;
408+
* function preload() {
409+
* myFont = loadFont('assets/fonts/inconsolata.ttf');
410+
* }
393411
* function setup() {
394412
* background(200);
395-
* var num1 = 11253106.115;
396-
* var num2 = -11253106.115;
413+
* var num1 = 321;
414+
* var num2 = -1321;
397415
*
398416
* noStroke();
399417
* fill(0);
400-
* textSize(12);
401-
* // Draw formatted numbers
418+
* textFont(myFont);
419+
* textSize(22);
420+
*
421+
* // nfs() aligns num1 (positive number) with num2 (negative number) by
422+
* // adding a blank space in front of the num1 (positive number)
423+
* // [left = 4] in num1 add one 0 in front, to align the digits with num2
424+
* // [right = 2] in num1 and num2 adds two 0's after both numbers
425+
* // To see the differences check the example of nf() too.
402426
* text(nfs(num1, 4, 2), 10, 30);
403-
*
404427
* text(nfs(num2, 4, 2), 10, 80);
405-
*
406428
* // Draw dividing line
407429
* stroke(120);
408430
* line(0, 50, width, 50);
@@ -411,7 +433,7 @@ function addNfp(num) {
411433
* </div>
412434
*
413435
* @alt
414-
* "11253106.11" top middle and "-11253106.11" displayed bottom middle
436+
* "0321.00" top middle and "-1321.00" displayed bottom middle
415437
*/
416438
/**
417439
* @method nfs

0 commit comments

Comments
 (0)