I'm trying to understand a rounding function I found in inherited JavaScript code:
function percentageWithCommas(x?) {
try {
return (x * 100).toLocaleString("en-UK", {
maximumFractionDigits: 1, minimumFractionDigits: 1 }) + '%';
} catch (e) {
return (x * 100).toFixed(2) + '%';
}
}
I understand that rounding in JS is nowadays done with .toLocaleString(...)
rather than .toFixed()
.
Why would one implement the same thing in both the try
and the catch
phrase?
Reference