Convert currency into number

  1. A user enters $100 into a textfield and submits, I need to strip off
    the $ sign before it gets saved to the db.

  2. The name of the field is “price”, why does params[:price] upon
    submitting have nil value?

Petr wrote:

  1. A user enters $100 into a textfield and submits, I need to strip off
    the $ sign before it gets saved to the db.

  2. The name of the field is “price”, why does params[:price] upon
    submitting have nil value?

  1. “$100.00”.scan(/[.0-9]/).to_s will return 100.00. If you don’t want
    decimal values then remove the period, but know that it will return
    10000

  2. You need the model in front of it. e.g.
    params[:your_model_name][:price]

Hope this helps. Perhaps there is a better way to achieve number one,
but that was the quickest thing I thought of.

Regards,

Michael

value = params[:price].gsub(/$$/, ‘’) # strip off a dollar sign if
it’s
the first character

  1. Not sure. What does you log say? is the param :price => $100 or is it
    :model_name => {:price => $100}

    View this message in context:
    http://www.nabble.com/Convert-currency-into-number-tf2043757.html#a5626980
    Sent from the RubyOnRails Users forum at Nabble.com.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs