Skip to content

Commit 0e3df94

Browse files
committed
fixed nf to work with negative number
1 parent fa3dbe5 commit 0e3df94

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/utilities/string_functions.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,20 +295,21 @@ p5.prototype.nf = function(nums, left, right) {
295295
};
296296

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

302+
300303
if (typeof right === 'undefined') {
301304
leftPart = leftPart.padStart(left, '0');
302-
return rightPart ? leftPart + '.' + rightPart : leftPart;
305+
let result = rightPart ? leftPart + '.' + rightPart : leftPart;
306+
return isNegative ? '-' + result : result;
303307
} else {
304308
let roundedOff = num.toFixed(right);
305309
[leftPart, rightPart] = roundedOff.toString().split('.');
306310
leftPart = leftPart.padStart(left, '0');
307-
if(typeof rightPart === 'undefined'){
308-
return leftPart;
309-
}else{
310-
return leftPart + '.' + rightPart;
311-
}
311+
let result = typeof rightPart === 'undefined' ? leftPart : leftPart + '.' + rightPart;
312+
return isNegative ? '-' + result : result;
312313
}
313314
}
314315

0 commit comments

Comments
 (0)