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

  • Home
  • SEARCH
  • 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 3397632
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:33:08+00:00 2026-05-18T04:33:08+00:00

So I have this hash below: a_hash = { 1 => one, 2 =>

  • 0

So I have this hash below:

   a_hash = {
    "1" => "one",
    "2" => "two",
    "3" => "three",
    "4" => "four",
    "5" => "five",
    "6" => "six",
    "7" => "seven",
    "8" => "eight",
    "9" => "nine",
    "10" => "ten",
    "11" => "eleven",
    "12" => "twelve",
    "13" => "thirteen",
    "14" => "fourteen",
    "15" => "fifteen",
    "16" => "sixteen",
    "17" => "seventeen",
    "18" => "eighteen",
    "19" => "nineteen",
    "20" => "twenty",
    "30" => "thirty",
    "40" => "forty",
    "50" => "fifty",
    "60" => "sixty",
    "70" => "seventy",
    "80" => "eighty",
    "90" => "ninety",
    "00" => "hundred", #not sure this is right 
    "000" => "thousand"  #not sure this is right 

    } 

Lets say my string input is “99100”.
Lets say I want my string output to be “ninty nine thousand one hundred”.
How do I go about using my hash above without typing each key/value. I was thinking maybe split my string at each char into an array….then for each number in that array return my value? Any other stratgies I should consider? This is what I have so far:

  puts "test the hash! Type a number hit enter"
  test_variable = gets.to_s.chomp
  puts a_hash[test_variable]

Post some code so I can try out. Thanks!

  • 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. Editorial Team
    Editorial Team
    2026-05-18T04:33:08+00:00Added an answer on May 18, 2026 at 4:33 am

    Short answer: don’t do this yourself, find a library that does it for you.

    However, it can be a cool exercise and it is actually an interesting problem. So, ignoring best practices of not reinventing the wheel… You’ll probably have to treat hundreds and thousands differently, because you can say “two hundred”, but “two seventy” doesn’t really make much sense.

    Here’s my poorly-tested, unoptimised attempt. It is a proof-of-concept and I’m pretty sure I have overlooked many cases. If you want more help, try reading other people’s source code.

    First we define two hashes, one for numbers, one for magnitudes (which are distinct because they can be prefixed with numbers in order to multiply them).

    class Integer
      NUMBERS = {
        1 => "one",
        2 => "two",
        3 => "three",
        4 => "four",
        5 => "five",
        6 => "six",
        7 => "seven",
        8 => "eight",
        9 => "nine",
        10 => "ten",
        11 => "eleven",
        12 => "twelve",
        13 => "thirteen",
        14 => "fourteen",
        15 => "fifteen",
        16 => "sixteen",
        17 => "seventeen",
        18 => "eighteen",
        19 => "nineteen",
        20 => "twenty",
        30 => "thirty",
        40 => "forty",
        50 => "fifty",
        60 => "sixty",
        70 => "seventy",
        80 => "eighty",
        90 => "ninety"
      }
    
      MAGNITUDES = {
        100 => "hundred",
        1000 => "thousand",
        1000_000 => "million"
      }
    end
    

    Next, define the conversion method.

    class Integer
      def to_text
        return nil if self == 0
        if NUMBERS.keys.include? self
          NUMBERS[self]
        elsif self < MAGNITUDES.keys.first
          base = maximum_fitting(NUMBERS, self)
          [NUMBERS[base], (self - base).to_text].compact * "-"
        else
          base = maximum_fitting(MAGNITUDES, self)
          quo, mod = self.divmod(base)
          [quo.to_text, MAGNITUDES[base], mod.to_text].compact * " "
        end
      end
    
      private
    
      def maximum_fitting(list, number)
        list.keys.select { |n| n <= number }.last
      end
    end
    

    To use it:

    puts 2351.to_text
    #=> "two thousand three hundred fifty-one"
    
    puts 14330132.to_text
    #=> "fourteen million three hundred thirty thousand one hundred thirty-two"
    
    puts 1000000.to_text
    #=> "one million"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I personally like option one below for maintainability but I could see option two
I have two tables, one has about 1500 records and the other has about
I have this code in jQuery, that I want to reimplement with the prototype
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have this line in a javascript block in a page: res = foo('<%=
I have this setup where in my development copy I can commit changes on
I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the
I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data

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.