Skip to content

Instantly share code, notes, and snippets.

@gdotdesign
Created August 27, 2012 12:24
Show Gist options
  • Select an option

  • Save gdotdesign/3487990 to your computer and use it in GitHub Desktop.

Select an option

Save gdotdesign/3487990 to your computer and use it in GitHub Desktop.
ConvertCurrency
convertCurrency = (input) ->
value = $(input).val().replace /[^\d\.]/g, ''
float = parseFloat(value)
float = 0 if isNaN(float)
precision = parseInt(input.attr('data-precision'))
if precision is 0
val = float.toString().replace /\.\d*/, ''
else
val = float.toString().replace new RegExp("(\\.\\d{#{precision}})\\d*"), '$1'
val = parseFloat val
$(input).val val || ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment