Back to all resources
currency formatting
error handling
validation

Number to Currency

By Jared Malan
This JavaScript function, formatCurrency, takes an input string, converts it to a floating-point number, and formats it as a US dollar currency string. The function first attempts to parse the input string into a number using parseFloat. If the input is not a valid number, it throws an error. If the input is valid, it formats the number as a currency string in US dollars without any decimal places using toLocaleString. The example usage demonstrates how to call the function and log the formatted currency string to the console.