Sha256: 70cfe804da52a11f3c8306d55682aa8a1f9b6b57377a360c33cdc66280a6a1ed

Contents?: true

Size: 909 Bytes

Versions: 18

Compression:

Stored size: 909 Bytes

Contents

module Spree
  class LocalizedNumber

    # Strips all non-price-like characters from the number, taking into account locale settings.
    def self.parse(number)
      return number unless number.is_a?(String)

      separator, delimiter = I18n.t([:'number.currency.format.separator', :'number.currency.format.delimiter'])
      non_number_characters = /[^0-9\-#{separator}]/

      # work on a copy, prevent original argument modification
      number = number.dup
      # strip everything else first, including thousands delimiter
      number.gsub!(non_number_characters, '')
      # then replace the locale-specific decimal separator with the standard separator if necessary
      number.gsub!(separator, '.') unless separator == '.'

      # Returns 0 to avoid ArgumentError: invalid value for BigDecimal(): "" for empty string
      return 0 unless number.present?

      number.to_d
    end

  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
spree_core-3.2.9 lib/spree/localized_number.rb
spree_core-3.3.6 lib/spree/localized_number.rb
spree_core-3.2.8 lib/spree/localized_number.rb
spree_core-3.3.5 lib/spree/localized_number.rb
spree_core-3.3.4 lib/spree/localized_number.rb
spree_core-3.2.7 lib/spree/localized_number.rb
spree_core-3.3.3 lib/spree/localized_number.rb
spree_core-3.2.6 lib/spree/localized_number.rb
spree_core-3.3.2 lib/spree/localized_number.rb
spree_core-3.2.5 lib/spree/localized_number.rb
spree_core-3.3.1 lib/spree/localized_number.rb
spree_core-3.2.4 lib/spree/localized_number.rb
spree_core-3.3.0 lib/spree/localized_number.rb
spree_core-3.3.0.rc4 lib/spree/localized_number.rb
spree_core-3.3.0.rc3 lib/spree/localized_number.rb
spree_core-3.2.3 lib/spree/localized_number.rb
spree_core-3.3.0.rc2 lib/spree/localized_number.rb
spree_core-3.3.0.rc1 lib/spree/localized_number.rb