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

Merged
merged 6 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 10 additions & 6 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
Contributor 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 Down Expand Up @@ -295,20 +298,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