Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 42745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:23:10+00:00 2026-05-10T15:23:10+00:00

I have a string, say ‘123’ , and I want to convert it to

  • 0

I have a string, say '123', and I want to convert it to the integer 123.

I know you can simply do some_string.to_i, but that converts 'lolipops' to 0, which is not the effect I have in mind. I want it to blow up in my face when I try to convert something invalid, with a nice and painful Exception. Otherwise, I can’t distinguish between a valid 0 and something that just isn’t a number at all.

EDIT: I was looking for the standard way of doing it, without regex trickery.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:23:11+00:00Added an answer on May 10, 2026 at 3:23 pm

    Ruby has this functionality built in:

    Integer('1001')                                    # => 1001   Integer('1001 nights')   # ArgumentError: invalid value for Integer: '1001 nights'   

    As noted in answer by Joseph Pecoraro, you might want to watch for strings that are valid non-decimal numbers, such as those starting with 0x for hex and 0b for binary, and potentially more tricky numbers starting with zero that will be parsed as octal.

    Ruby 1.9.2 added optional second argument for radix so above issue can be avoided:

    Integer('23')                                     # => 23 Integer('0x23')                                   # => 35 Integer('023')                                    # => 19 Integer('0x23', 10) # => #<ArgumentError: invalid value for Integer: '0x23'> Integer('023', 10)                                # => 23 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string say walk talk, can't won't Won't woN'T talk. I want to
I have a array of string say: String[] Fields=new String[]{RowField,RowField1} In which I can
Okay. Say I have string '193' And I want to remove the last numbers
I have string containg anonymus function definition, but how i can call this. Lets
I have a string which has say 3000 characters, now I want to split
I have java String of say length 10 .Now i want to reduce it
I have a string let's say http://someUrul.com/someController/SOmeAction?SomeQS=http://someOtherUrl and I want to replace the first
hey guys, i have a string which contains say 100 words, now i want
Let's say we have string like P\u0159\u00edprava How can we print it out with
i have String from php, let say stringName={instruction:[1,2,3]} and i want to turn tat

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.