9

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.

1 Answer 1

14

(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")
Sign up to request clarification or add additional context in comments.

2 Comments

You actually cant for X number of days (now elapsed)... besides I figured I'd give others a chance to contribute.
BigDecimal.new is deprecated; use BigDecimal() method instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.