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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
(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: