Skip to content

Commit

Permalink
Fix issue where nf with 0 'right' parameter returns undefined in string
Browse files Browse the repository at this point in the history
  • Loading branch information
limzykenneth committed Jul 25, 2023
1 parent 46f713a commit 6c85a0d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/utilities/string_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ function doNf(num, left, right) {
let roundedOff = num.toFixed(right);
[leftPart, rightPart] = roundedOff.toString().split('.');
leftPart = leftPart.padStart(left, '0');
return leftPart + '.' + rightPart;
if(typeof rightPart === 'undefined'){
return leftPart;
}else{
return leftPart + '.' + rightPart;
}
}
}

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 @@ -105,6 +105,12 @@ suite('String functions', function() {
result = myp5.nf(num, '3', '4'); // automatic conversion?
assert.equal(result, '31415160.0000');
});

test('should return correct string', function() {
var num = 123.45;
result = myp5.nf(num, 3, 0);
assert.equal(result, '123');
});
});

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

0 comments on commit 6c85a0d

Please sign in to comment.