Created
June 16, 2016 14:30
-
-
Save fupslot/e3b42a0a4da8f7ed7a80fe4ffbe125c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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