views:

125

answers:

5

i have

string = "$575.00 "
string.to_f
// => 0.0

string = "575.00 "
string.to_f
// => 575.0

the value coming in is in this format and i need to insert into a database field that is decimal any suggestions

"$575.00 " 
A: 

you can use regex for that:

s/^.//

As laways, this is PCRE syntax.

In Ruby, you can use the sub() method of the string class to replace the string:

result = string.sub(/^./,"")

This should work.

[EDIT]

Ok, someone asked what's the gsub() is for:

gsub() acts like sub() but with the /g modifier in PCRE (for global replacement):

s/a/b/

in PCRE is

string.sub(/a/, "b")

and

s/a/b/g

is

string.gsub(/a/, "b")

in Ruby

polemon
A: 

You could try something like this.

string = string[1..-1] if string.match(/^\$/)

Or this.

string.gsub!(/^\$/, '')

Remember to put that backslash in your Regexp, it also means "end of string."

AboutRuby
+5  A: 

We did this so often we wrote an extension to String called cost_to_f:

class String
  def cost_to_f
    self.delete('$,').to_f
  end
end

We store such extensions in config/initializers/extensions/string.rb.

You can then simply call:

"$5,425.55".cost_to_f   #=> 5425.55

If you are using this method rarely, the best bet is to simply create a function, since adding functions to core classes is not exactly something I would recommend lightly:

def cost_to_f(string)
  string.delete('$,').to_f
end

If you need it in more than one class, you can always put it in a module, then include that module wherever you need it.


One more tidbit. You mentioned that you need to process this string when it is being written to the database. With ActiveRecord, the best way to do this is:

class Item < ActiveRecord::Base
  def price=(p)
    p = p.cost_to_f if p.is_a?(String)  
    write_attribute(:price, p)
  end
end

EDIT: Updated to use String#delete!

wuputah
A: 

What I'd use (instead of regular expressions) is simply the built-in slice! method in the String class. For example,

s = "Some string"
s.slice!(0) # Deletes and returns the 0th character from the string. 
s # => "ome string"

Documentation here.

sluukkonen
+3  A: 

So many answers... i'll try to summarize all that are available now, before give own answer.

1. string.gsub(/[\$,]/, '')
   string.gsub!(/^\$/, '')
2. string[1..-1]
3. string.slice(0) # => "ome string"
4. s/^.//
  1. Why (g)sub and regexp Just for deleting a character? String#tr is faster and shorter. String#delete is even better.
  2. Good, fast, simple. Power of reverse indexing.
  3. Hm... looks like it returns "S". Because it is an alias to String#[]
  4. Perl? /me is cheking question tags...

And my advice is: What if you have not dollar, but yena? Or what if you don't even have anything before numbers? So i'll prefer:

string[/\d.+/]

This will crop leading non-decimal symbols, that prevent to_f to work well.

P.S.: By the way. It's known, that float is bad practice for storing money amounts. http://stackoverflow.com/questions/61872/use-float-or-decimal-for-accounting-application-dollar-amount

Nakilon
gsub() acts like the `/g` modifier of PCRE: s/a/b/is string.sub(/a/, "b")and s/a/b/gis string.gsub(/a/, "b")in Ruby
polemon
I know well perl's `s//` and ruby's `gsub`. But question is about ruby.
Nakilon
What if you have commas in your value? `delete(",$")` is a better solution than our `gsub` though, thanks for pointing that out. Leave a comment next time!
wuputah