Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed nf to work with negative number #7054

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/utilities/string_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ p5.prototype.matchAll = function(str, reg) {
* then unused decimal places will be set to 0. For example, calling
* `nf(123.45, 4, 3)` returns the string `'0123.450'`.
*
* When the number is negative, for example, calling `nf(-123.45, 5, 2)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding that. Just one last thought: could you also modify the reference example so that it appears like this?

image

Since we are not counting the minus sign as an extra digit, negative numbers may have greater width due to the additional minus sign. Therefore, we could show users that negative numbers occupy more space as we are not counting it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya no problem.

* returns the string `'-00123.45'`.
*
* @method nf
* @param {Number|String} num number to format.
* @param {Integer|String} [left] number of digits to include to the left of
Expand All @@ -247,21 +250,24 @@ p5.prototype.matchAll = function(str, reg) {
*
* // Display the number as a string.
* let formatted = nf(number);
* text(formatted, 20, 25);
* text(formatted, 20, 20);
*
* let negative = nf(-number, 4, 2);
* text(negative, 20, 40);
*
* // Display the number with four digits
* // to the left of the decimal.
* let left = nf(number, 4);
* text(left, 20, 50);
* text(left, 20, 60);
*
* // Display the number with four digits
* // to the left of the decimal and one
* // to the right.
* let right = nf(number, 4, 1);
* text(right, 20, 75);
* text(right, 20, 80);
*
* describe(
* 'The numbers "123.45", "0123.45", and "0123.5" written on three separate lines. The text is in black on a gray background.'
* 'The numbers "123.45", "-0123.45", "0123.45", and "0123.5" written on four separate lines. The text is in black on a gray background.'
* );
* }
* </code>
Expand Down Expand Up @@ -295,20 +301,21 @@ p5.prototype.nf = function(nums, left, right) {
};

function doNf(num, left, right) {
let isNegative = num < 0;
num = Math.abs(num);
let [leftPart, rightPart] = num.toString().split('.');


if (typeof right === 'undefined') {
leftPart = leftPart.padStart(left, '0');
return rightPart ? leftPart + '.' + rightPart : leftPart;
let result = rightPart ? leftPart + '.' + rightPart : leftPart;
return isNegative ? '-' + result : result;
} else {
let roundedOff = num.toFixed(right);
[leftPart, rightPart] = roundedOff.toString().split('.');
leftPart = leftPart.padStart(left, '0');
if(typeof rightPart === 'undefined'){
return leftPart;
}else{
return leftPart + '.' + rightPart;
}
let result = typeof rightPart === 'undefined' ? leftPart : leftPart + '.' + rightPart;
return isNegative ? '-' + result : result;
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/unit/utilities/string_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ suite('String functions', function() {
result = myp5.nf(num, 3, 0);
assert.equal(result, '123');
});

test('should return correct string', function() {
var num = -123;
result = myp5.nf(num, 5);
assert.equal(result, '-00123');
});
});

suite('p5.prototype.nfc', function() {
Expand Down