Hel;p with Tip Calculator

Discussion in 'Ruby' started by Hd Pwnz0r, Aug 8, 2010.

  1. Hd Pwnz0r

    Hd Pwnz0r Guest

    puts "What is the total?"
    input = gets.chomp.to_i
    puts "What percent do you want to tip?"
    input2 = gets.chomp.to_i
    percent = 100
    tip = input2 * input * 0.01
    puts "You should tip $#{tip}"

    This is a tip calculator. I want it to work if the person enters $45 or
    15%.

    Right now, it would only return 0 because they are invalid numbers. It
    would only work with an input of 15 or 45. I have really no clue how to
    do that.
    --
    Posted via http://www.ruby-forum.com/.
     
    Hd Pwnz0r, Aug 8, 2010
    #1
    1. Advertising

  2. On Mon, 9 Aug 2010 00:46:29 +0900
    Hd Pwnz0r <> wrote:

    > puts "What is the total?"
    > input = gets.chomp.to_i
    > puts "What percent do you want to tip?"
    > input2 = gets.chomp.to_i
    > percent = 100
    > tip = input2 * input * 0.01
    > puts "You should tip $#{tip}"
    >
    > This is a tip calculator. I want it to work if the person enters $45
    > or 15%.
    >
    > Right now, it would only return 0 because they are invalid numbers. It
    > would only work with an input of 15 or 45. I have really no clue how
    > to do that.


    Use

    '45 %'.sub(/%/, '').strip
    '14 $'.sub(/[$]/, '').strip

    for that.

    "strip" can remove unwanted trailing and leading blanks.
    "sub" substitutes the charachters in the RE with an empty string
    removing them.

    Regards,
    Andrei
     
    Andrei Beliankou, Aug 8, 2010
    #2
    1. Advertising

  3. On Sun, Aug 8, 2010 at 8:59 AM, Andrei Beliankou <> wrote:

    > Use
    >
    > '45 %'.sub(/%/, '').strip
    > '14 $'.sub(/[$]/, '').strip


    More universally

    input_string.gsub(/\D/, '')

    removes any non-[0-9] characters, including white space.

    --
    Hassan Schroeder ------------------------
    twitter: @hassan
     
    Hassan Schroeder, Aug 8, 2010
    #3
  4. Hd Pwnz0r

    Hd Pwnz0r Guest

    Hassan Schroeder wrote:
    > On Sun, Aug 8, 2010 at 8:59 AM, Andrei Beliankou <>
    > wrote:
    >
    >> Use
    >>
    >> '45 %'.sub(/%/, '').strip
    >> '14 $'.sub(/[$]/, '').strip

    >
    > More universally
    >
    > input_string.gsub(/\D/, '')
    >
    > removes any non-[0-9] characters, including white space.


    I get:

    tax.rb:6:in `<main>': undefined method `gsub' for 0:Fixnum
    (NoMethodError)

    When using that code.

    As for Andrei, thanks, that's a good quick fix.
    --
    Posted via http://www.ruby-forum.com/.
     
    Hd Pwnz0r, Aug 10, 2010
    #4
  5. On Tue, Aug 10, 2010 at 6:51 AM, Hd Pwnz0r
    <> wrote:

    >> =A0 input_string.gsub(/\D/, '')


    > tax.rb:6:in `<main>': undefined method `gsub' for 0:Fixnum
    > (NoMethodError)
    >
    > When using that code.


    Yes, it works on *strings* -- that was the problem you posed. If you
    already have a *number*, it's not going to include any symbols like
    "$" or "%" in the first place.

    --=20
    Hassan Schroeder ------------------------
    twitter: @hassan
     
    Hassan Schroeder, Aug 10, 2010
    #5
  6. Hd Pwnz0r

    jason joo Guest

    [Note: parts of this message were removed to make it a legal post.]

    or just
    >> input_string.to_s.gsub(/\D/,'')

    for number format permitted

    2010/8/10 Hassan Schroeder <>

    > On Tue, Aug 10, 2010 at 6:51 AM, Hd Pwnz0r
    > <> wrote:
    >
    > >> input_string.gsub(/\D/, '')

    >
    > > tax.rb:6:in `<main>': undefined method `gsub' for 0:Fixnum
    > > (NoMethodError)
    > >
    > > When using that code.

    >
    > Yes, it works on *strings* -- that was the problem you posed. If you
    > already have a *number*, it's not going to include any symbols like
    > "$" or "%" in the first place.
    >
    > --
    > Hassan Schroeder ------------------------
    > twitter: @hassan
    >
    >
     
    jason joo, Aug 11, 2010
    #6
  7. On Tue, Aug 10, 2010 at 6:38 PM, jason joo <> wrote:
    > or just
    >>> input_string.to_s.gsub(/\D/,'')

    > for number format permitted


    Depending on what your actual numeric use case is :)

    $ irb
    >> input = 9

    => 9
    >> input.to_s.gsub(/\D/,'')

    => "9"
    >> input = 9.095

    => 9.095
    >> input.to_s.gsub(/\D/,'')

    => "9095"
    >>


    --
    Hassan Schroeder ------------------------
    twitter: @hassan
     
    Hassan Schroeder, Aug 11, 2010
    #7
  8. Hd Pwnz0r

    jason joo Guest

    [Note: parts of this message were removed to make it a legal post.]

    aha, i forgot positive number and dot. then the regular expression should
    be /([\-+]?\d*[.]?\d*)/

    eg

    if input_string =~ /([\-+]?\d*[.]?\d*)/
    input_string = $1
    else
    #have no number part
    input_string = 0
    end

    9 => "9"
    -9 => "-9"
    -9.098 => "-9.098"

    2010/8/11 Hassan Schroeder <>

    > On Tue, Aug 10, 2010 at 6:38 PM, jason joo <> wrote:
    > > or just
    > >>> input_string.to_s.gsub(/\D/,'')

    > > for number format permitted

    >
    > Depending on what your actual numeric use case is :)
    >
    > $ irb
    > >> input = 9

    > => 9
    > >> input.to_s.gsub(/\D/,'')

    > => "9"
    > >> input = 9.095

    > => 9.095
    > >> input.to_s.gsub(/\D/,'')

    > => "9095"
    > >>

    >
    > --
    > Hassan Schroeder ------------------------
    > twitter: @hassan
    >
    >
     
    jason joo, Aug 11, 2010
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmV6YSBTb2xvdWtp?=

    Not able to populate and select an item in a list dynamically? Hel

    =?Utf-8?B?UmV6YSBTb2xvdWtp?=, Aug 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    371
    Scott Allen
    Aug 25, 2004
  2. Piotre Ugrumov

    Hel me! I'm going mad!!!

    Piotre Ugrumov, Feb 15, 2004, in forum: Java
    Replies:
    0
    Views:
    404
    Piotre Ugrumov
    Feb 15, 2004
  3. Piotre Ugrumov

    Hel me! I'm going mad!!!

    Piotre Ugrumov, Feb 15, 2004, in forum: Java
    Replies:
    2
    Views:
    394
    Steve Horsley
    Feb 18, 2004
  4. David Mark
    Replies:
    16
    Views:
    932
    Scott Sauyet
    Nov 11, 2011
  5. David Mark
    Replies:
    58
    Views:
    1,495
    David Mark
    Dec 6, 2011
Loading...

Share This Page