Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Hel;p with Tip Calculator

Reply
Thread Tools

Hel;p with Tip Calculator

 
 
Hd Pwnz0r
Guest
Posts: n/a
 
      08-08-2010
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/.

 
Reply With Quote
 
 
 
 
Andrei Beliankou
Guest
Posts: n/a
 
      08-08-2010
On Mon, 9 Aug 2010 00:46:29 +0900
Hd Pwnz0r <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Hassan Schroeder
Guest
Posts: n/a
 
      08-08-2010
On Sun, Aug 8, 2010 at 8:59 AM, Andrei Beliankou <(E-Mail Removed)> 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 ------------------------ http://www.velocityreviews.com/forums/(E-Mail Removed)
twitter: @hassan

 
Reply With Quote
 
Hd Pwnz0r
Guest
Posts: n/a
 
      08-10-2010
Hassan Schroeder wrote:
> On Sun, Aug 8, 2010 at 8:59 AM, Andrei Beliankou <(E-Mail Removed)>
> 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/.

 
Reply With Quote
 
Hassan Schroeder
Guest
Posts: n/a
 
      08-10-2010
On Tue, Aug 10, 2010 at 6:51 AM, Hd Pwnz0r
<(E-Mail Removed)> 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 ------------------------ (E-Mail Removed)
twitter: @hassan

 
Reply With Quote
 
jason joo
Guest
Posts: n/a
 
      08-11-2010
[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 <(E-Mail Removed)>

> On Tue, Aug 10, 2010 at 6:51 AM, Hd Pwnz0r
> <(E-Mail Removed)> 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 ------------------------ (E-Mail Removed)
> twitter: @hassan
>
>


 
Reply With Quote
 
Hassan Schroeder
Guest
Posts: n/a
 
      08-11-2010
On Tue, Aug 10, 2010 at 6:38 PM, jason joo <(E-Mail Removed)> 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 ------------------------ (E-Mail Removed)
twitter: @hassan

 
Reply With Quote
 
jason joo
Guest
Posts: n/a
 
      08-11-2010
[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 <(E-Mail Removed)>

> On Tue, Aug 10, 2010 at 6:38 PM, jason joo <(E-Mail Removed)> 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 ------------------------ (E-Mail Removed)
> twitter: @hassan
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
David Mark's Javascript Tip Du Jour - Volume #1 - Tip #1234 - How toMeasure Element Dimensions David Mark Javascript 58 12-06-2011 10:13 PM
David Mark's Javascript Tip of the Day - Volume #1 - #Tip 14-C David Mark Javascript 16 11-11-2011 02:45 AM
calculator makko Perl 2 03-01-2005 06:06 AM
Re: distance calculator =?Utf-8?B?bWFoc2E=?= ASP .Net 3 06-05-2004 09:31 PM
bandwidth usage calculator Sai Cisco 5 11-07-2003 08:18 PM



Advertisments