From: Hd Pwnz0r on
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/.

From: Andrei Beliankou on
On Mon, 9 Aug 2010 00:46:29 +0900
Hd Pwnz0r <human.dictionary(a)rocketmail.com> 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

From: Hassan Schroeder on
On Tue, Aug 10, 2010 at 6:51 AM, Hd Pwnz0r
<human.dictionary(a)rocketmail.com> 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 ------------------------ hassan.schroeder(a)gmail.com
twitter: @hassan

From: jason joo on
[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 <hassan.schroeder(a)gmail.com>

> On Tue, Aug 10, 2010 at 6:51 AM, Hd Pwnz0r
> <human.dictionary(a)rocketmail.com> 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 ------------------------ hassan.schroeder(a)gmail.com
> twitter: @hassan
>
>

From: Hassan Schroeder on
On Tue, Aug 10, 2010 at 6:38 PM, jason joo <jasonjoo.god(a)gmail.com> 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 ------------------------ hassan.schroeder(a)gmail.com
twitter: @hassan

 |  Next  |  Last
Pages: 1 2
Prev: New in classes
Next: Attributes in class