views:

202

answers:

1

I have a Ruby String that contains decimal-containing numbers. What's the best way to convert this into a decimal (fixed-point)?

"Best" means:

  • Reliable given a wide range of input options
  • Simple to write
  • Easy to read
  • DRY

...in roughly that order.

+2  A: 

(Found it myself after a bit of digging)

BigDecimal is the standard Ruby fixed-point type, and it's constructor takes a String and handles the parsing for you:

BigDecimal.new("123.45")
Craig Walker
Good job. Now accept your own answer ;-)
Trevoke
You actually cant for X number of days (now elapsed)... besides I figured I'd give others a chance to contribute.
Craig Walker