Sha256: d68be582288d2800e3947a3a58083b9d5a8d40b875abdd6de92bfb3c10edb592

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

# Like val(), but return the value parsed as float (or 0, if the value is not numeric). Also works for non-input elements (using the element's text content as value).
#
# Thousands delimiters (",") are handled correctly. 
# German thousands delimiters (".") and separators (",") are handled correctly if the lang attribute of the input element (or any of its parent elements) is set to "de".
#
# Options:
#   ignoreCurrencySign: If true, values with leading currency signs (€, $) are considered numeric and parsed correctly.
#   nullOnError: If true, null is returned (instead of 0) if the value is not numeric.

$.fn.numericValue = (options = {}) ->
  
  delocalize = (val, locale) ->
    if locale == "de"
      val.replace(".", "").replace(",", ".")
    else
      val.replace(",", "")
  
  $this = $(this)
  locale = $this.closest("[lang]").attr("lang")
  val = if $this.is(":input") then $this.val() else $this.text()
  val = delocalize(val, locale)
  val = val.replace(/^\s*[€$]\s*/, "") if options.ignoreCurrencySign
  if $.isNumeric(val)
    parseFloat(val)
  else
    if options.nullOnError then null else 0

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
da-js-1.2.1 lib/assets/javascripts/da-js/numeric_value.js.coffee
da-js-1.2.0 lib/assets/javascripts/da-js/numeric_value.js.coffee
da-js-1.1.2 lib/assets/javascripts/da-js/numeric_value.js.coffee
da-js-1.1.1 lib/assets/javascripts/da-js/numeric_value.js.coffee
da-js-1.1.0 lib/assets/javascripts/da-js/numeric_value.js.coffee