Craig
04/04/2025, 8:58 PMfunction preciseRound(num, decimals = 2) {
const factor = Math.pow(10, decimals);
return Math.round(num * factor) / factor
}
Tell me why I'm wrong for doing this (I have no idea if it will have side effects).
The challenge was to make 1405.985 round to 1405.99
toFixed(2) was rounding it to 1405.98. I have a feeling this is something to do with floating point in binary.eblackey
04/04/2025, 9:07 PMalien4u
04/04/2025, 9:09 PMCraig
04/04/2025, 9:14 PMalien4u
04/04/2025, 9:15 PMCraig
04/04/2025, 9:16 PMalien4u
04/04/2025, 9:19 PMalien4u
04/04/2025, 9:19 PMCraig
04/04/2025, 9:19 PMCraig
04/04/2025, 9:19 PMalien4u
04/04/2025, 9:20 PMCraig
04/04/2025, 9:20 PMCraig
04/04/2025, 9:28 PMconst roundByExponentWithLogs = (pValue, pDecimals) => {
log.debug('before rounding', pValue);
const rounded = Number(Math.round(pValue + 'e' + pDecimals) + 'e-' + pDecimals);
log.debug('after rounding', rounded);
return rounded
}
Craig
04/04/2025, 9:28 PMAnthony OConnor
04/04/2025, 9:44 PMMath.round((num + Number.EPSILON) * 100) / 100;
alien4u
04/04/2025, 9:57 PMAnthony OConnor
04/04/2025, 9:59 PMAnthony OConnor
04/04/2025, 10:00 PMalien4u
04/04/2025, 10:04 PMAnthony OConnor
04/04/2025, 10:06 PMnum
it becomes a different number that rounds differentlyAnthony OConnor
04/04/2025, 10:06 PMAnthony OConnor
04/04/2025, 10:06 PMAnthony OConnor
04/04/2025, 10:08 PMalien4u
04/04/2025, 10:09 PMAnthony OConnor
04/04/2025, 10:12 PMAnthony OConnor
04/04/2025, 10:12 PMCraig
04/04/2025, 10:14 PMerictgrubaugh
04/04/2025, 10:14 PMJavaScript is bad by design with numbersJust because this is always a bee in my bonnet, this is not a JavaScript-specific problem, but a universal problem with the IEEE standard representation of floating point numbers. Other languages have more native mechanics for dealing with it, but they all have to deal with it.
Anthony OConnor
04/04/2025, 10:14 PMAnthony OConnor
04/04/2025, 10:15 PMerictgrubaugh
04/04/2025, 10:15 PMalien4u
04/04/2025, 10:19 PMerictgrubaugh
04/04/2025, 10:21 PMAnthony OConnor
04/04/2025, 10:21 PMAnthony OConnor
04/04/2025, 10:23 PMconst daysToIncrement = threshold * multiplier;
const lastDate = new Date (data.values.edited);
let nextDate = new Date (lastDate);
nextDate.setDate(lastDate.getDate() + daysToIncrement);
return nextDate;
alien4u
04/04/2025, 10:24 PMerictgrubaugh
04/04/2025, 10:25 PMerictgrubaugh
04/04/2025, 10:25 PMAnthony OConnor
04/04/2025, 10:26 PMalien4u
04/04/2025, 10:27 PMAnthony OConnor
04/04/2025, 10:27 PMAnthony OConnor
04/04/2025, 10:27 PMfunction calculateTermInMonths({startDate, endDate}){
const startDay = startDate.getDate();
const startMon = startDate.getMonth();
const startYr = startDate.getFullYear();
const endDay = endDate.getDate();
const endMon = endDate.getMonth();
const endYr = endDate.getFullYear();
const yrDiff = endYr - startYr;
const monDiff = endMon - startMon;
const dayDiff = endDay - startDay;
const daysInEndMonth = new Date(endYr, endMon+1, 0).getDate();
const frac = (dayDiff + 1)/daysInEndMonth;
return (yrDiff * 12) + monDiff + frac;
}
function calculateEndDate({startDate, term}){
let endDate = new Date(startDate);
endDate = new Date( endDate.setMonth(endDate.getMonth() + term));
const frac = term.toString().split('.')[1];
if(frac) {
const endMon = endDate.getMonth();
const endYr = endDate.getFullYear();
const daysInEndMonth = new Date(endYr, endMon+1, 0).getDate();
const daysToAdd = Number(( daysInEndMonth * parseFloat('0.' + frac )).toFixed());
endDate = new Date( endDate.setDate( endDate.getDate() + daysToAdd ));
}
endDate = new Date( endDate.setDate( endDate.getDate() - 1));
return endDate;
}
Anthony OConnor
04/04/2025, 10:28 PMAnthony OConnor
04/04/2025, 10:29 PMCraig
04/04/2025, 10:38 PMAnthony OConnor
04/04/2025, 10:43 PMCraig
05/12/2025, 11:26 PMAnthony OConnor
05/13/2025, 1:15 AMconst betterRounding = (pValue, pDecimals) => {
return Math.round((pValue + Number.EPSILON) * Math.pow(10,pDecimals)) / Math.pow(10,pDecimals);
};