Skip to content

Instantly share code, notes, and snippets.

@Reelix
Created November 23, 2016 22:33
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 Reelix/ba24fb564c4d004756949cdd04d74384 to your computer and use it in GitHub Desktop.
Save Reelix/ba24fb564c4d004756949cdd04d74384 to your computer and use it in GitHub Desktop.
<script>
function formatCurrency()
{
// Get the values from the text-boxes
var symbol = document.getElementById('formatCurrencySymbol').value;
var number = parseFloat(document.getElementById('formatCurrencyNumber').value);
var decimalPlaces = document.getElementById('formatCurrencyDecimalPlaces').value;
// Do the formatting - Regex here!
var result = symbol + number.toFixed(decimalPlaces).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
// Display the result
alert(result);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment