Skip to content

Instantly share code, notes, and snippets.

@fupslot
Created June 16, 2016 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fupslot/e3b42a0a4da8f7ed7a80fe4ffbe125c4 to your computer and use it in GitHub Desktop.
Save fupslot/e3b42a0a4da8f7ed7a80fe4ffbe125c4 to your computer and use it in GitHub Desktop.
function safeParse(n) {
n = +n;
if (isNaN(n)) throw new TypeError('NANUMERR');
if (0 > n) throw new TypeError('NEGNUMERR');
return n
}
function safeFixed(n) {
return (safeParse(n)).toFixed('2');
}
try {
console.log(safeFixed(3));
console.log(safeFixed(-3.12));
console.log(safeFixed('-3.4'));
console.log(safeFixed(3.987643));
// console.log(safeParseFloat(-3.4));
// console.log(safeParseFloat('-3.4'));
// console.log(safeParseFloat('-3,4'));
// console.log(safeParseFloat('-3b4'));
// console.log(safeParseFloat('#3b4'));
console.log(safeFixed('#3b4'));
} catch (ex) {
console.log(ex.message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment