# Hel;p with Tip Calculator

**URL:** <https://www.ruby-forum.com/t/hel-p-with-tip-calculator/192929>\
**Category:** Ruby\
**Created:** [August 8, 2010, 5:46pm UTC](https://www.ruby-forum.com/t/hel-p-with-tip-calculator/192929 "2010-08-08T17:46:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![humandictionary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@humandictionary](https://www.ruby-forum.com/u/humandictionary)\
**Post date:** [August 8, 2010, 5:46pm UTC](https://www.ruby-forum.com/t/hel-p-with-tip-calculator/192929/1 "2010-08-08T17:46:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![humandictionary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@humandictionary](https://www.ruby-forum.com/u/humandictionary)\
**Post date:** [August 8, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/hel-p-with-tip-calculator/192929/2 "2010-08-08T18:00:07Z")

</div>

On Mon, 9 Aug 2010 00:46:29 +0900  
Hd Pwnz0r [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![humandictionary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@humandictionary](https://www.ruby-forum.com/u/humandictionary)\
**Post date:** [August 8, 2010, 6:20pm UTC](https://www.ruby-forum.com/t/hel-p-with-tip-calculator/192929/3 "2010-08-08T18:20:36Z")

</div>

On Sun, Aug 8, 2010 at 8:59 AM, Andrei B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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

More universally

input\_string.gsub(/\D/, ‘’)

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

---

<div class="post-metadata">

**Author:** ![humandictionary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@humandictionary](https://www.ruby-forum.com/u/humandictionary)\
**Post date:** [August 10, 2010, 3:51pm UTC](https://www.ruby-forum.com/t/hel-p-with-tip-calculator/192929/4 "2010-08-10T15:51:44Z")

</div>

Hassan S. wrote:

> On Sun, Aug 8, 2010 at 8:59 AM, Andrei B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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.

---

<div class="post-metadata">

**Author:** ![humandictionary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@humandictionary](https://www.ruby-forum.com/u/humandictionary)\
**Post date:** [August 10, 2010, 3:58pm UTC](https://www.ruby-forum.com/t/hel-p-with-tip-calculator/192929/5 "2010-08-10T15:58:31Z")

</div>

On Tue, Aug 10, 2010 at 6:51 AM, Hd Pwnz0r  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![humandictionary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@humandictionary](https://www.ruby-forum.com/u/humandictionary)\
**Post date:** [August 11, 2010, 3:38am UTC](https://www.ruby-forum.com/t/hel-p-with-tip-calculator/192929/6 "2010-08-11T03:38:46Z")

</div>

or just

> > input\_string.to\_s.gsub(/\D/,‘’)  
> > for number format permitted

2010/8/10 Hassan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![humandictionary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@humandictionary](https://www.ruby-forum.com/u/humandictionary)\
**Post date:** [August 11, 2010, 6:19am UTC](https://www.ruby-forum.com/t/hel-p-with-tip-calculator/192929/7 "2010-08-11T06:19:08Z")

</div>

On Tue, Aug 10, 2010 at 6:38 PM, jason joo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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”

---

<div class="post-metadata">

**Author:** ![humandictionary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@humandictionary](https://www.ruby-forum.com/u/humandictionary)\
**Post date:** [August 11, 2010, 7:05am UTC](https://www.ruby-forum.com/t/hel-p-with-tip-calculator/192929/8 "2010-08-11T07:05:22Z")

</div>

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 S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![humandictionary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@humandictionary](https://www.ruby-forum.com/u/humandictionary)\
**Post date:** [August 11, 2010, 8:15pm UTC](https://www.ruby-forum.com/t/hel-p-with-tip-calculator/192929/9 "2010-08-11T20:15:42Z")

</div>

At 2010-08-11 01:03AM, “jason joo” wrote:

> [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\*)/

The empty string matches that pattern (all your quantifiers allow zero  
of the previous item). You want:

```
/[-+]?(?:\d+(\.\d*)?)|(?:\d*\.\d+)/

```

where there must be at least one digit.
