Convert 125% into 1.25?

irb(main):011:0> format("%.2f",125)
=> “125.00”
irb(main):012:0> format("%.2f",125%)
SyntaxError: compile error
(irb):12: syntax error, unexpected ‘)’

From a mathematical aspect the % sign means 1/100 so 125 % equals
1.25.

Pen T. schrieb (am 21.8.10 07:16):

irb(main):011:0> format("%.2f",125)
=> “125.00”

irb(main):012:0> format("%.2f",125%)
SyntaxError: compile error
(irb):12: syntax error, unexpected ‘)’

Ruby does not allow you to write 125% like that.
You can however divide 125 by 100 (or rather 100.0 to get a
floating point number):

format( “%.2f”, 125 / 100.0 )
=> “1.25”

Regards,
Philipp

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Pen,

On Saturday 21 August 2010, Pen T. [email protected] wrote:

irb(main):012:0> format("%.2f",125%)

125% is, strictly spoken, not a number, but a number plus a unit. The
unit
is a character, not an integer, and this makes “125%” only valid as a
string
in Ruby. You’ve got to remove the “%” sign before you can treat “125” as
a
number.

HTH.

  Eric

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkxvzWMACgkQhS0drJ3goJJmWwCaA1o2qvv0Tf1L4bsnAw7HJ95D
xdUAoIbg/9wV+D1bdTBIUXsOL38dLqSy
=SiNK
-----END PGP SIGNATURE-----

On Aug 22, 2010, at 13:32 , Dick D. wrote:

?> class Fixnum

def percent

the appropriate place to put that is Integer, not Fixnum.

You can sort of cheat, thanks to Rubys open classes.
Just add a method to Fixnum:

~:) irb

?> 100.class
=> Fixnum

?> class Fixnum

def percent
return self.to_f / 100
end
end
=> nil

?> 100.percent
=> 1.0

125.percent
=> 1.25

quit
~:)

This is pretty evil, though :slight_smile:

It is actually possible to define a method called ‘%’, but then you have
to call it as

100.%()

which looks awful.

On Sat, Aug 21, 2010 at 1:58 PM, Eric MSP Veith

On Aug 22, 2010, at 7:59 PM, Ryan D. wrote:

On Aug 22, 2010, at 13:32 , Dick D. wrote:

?> class Fixnum

def percent

the appropriate place to put that is Integer, not Fixnum.

% ruby -e ‘p 100.class.superclass’
Integer

Or even Numeric:

irb> 1.class.ancestors
=> [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]
irb> class Numeric
irb> def percent
irb> self.to_f / 100.0
irb> end
irb> end
=> nil
irb> 5.percent
=> 0.05
irb> 125.percent
=> 1.25
irb> 14.7.percent
=> 0.147
irb> (99+“44/100”.to_r).percent
=> 0.9944

-Rob

Rob B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

Ah, good catch both, thanks.

On Mon, Aug 23, 2010 at 2:02 AM, Rob B.

On Mon, Aug 23, 2010 at 8:22 AM, Ryan D.
[email protected]wrote:

On Aug 22, 2010, at 18:02 , Rob B. wrote:

Or even Numeric:
irb> class Numeric
irb> def percent
irb> self.to_f / 100.0
irb> end
irb> end

I said Integer because of how the original problem was described. I
thought

about floats and decided quickly that it didn’t make sense… but looking at
your examples above, that actually works and reads pretty well.

As a suggestion, if Rational is being used maybe “override” that with
the
following to avoid rounding differences: typically interest rates as
percentages have the fractional part as 1/2, 3/4, 5/8, etc, so mostly do
get
“mapped” to “precise” floats, but there might be uses of 2/5, etc
class Rational
def percent; self / 100; end
end
class Integer
def percent; Rational( self, 100); end
end

On Aug 22, 2010, at 18:02 , Rob B. wrote:

=> nil
irb> 5.percent
=> 0.05
irb> 125.percent
=> 1.25
irb> 14.7.percent
=> 0.147
irb> (99+“44/100”.to_r).percent
=> 0.9944

I said Integer because of how the original problem was described. I
thought about floats and decided quickly that it didn’t make sense…
but looking at your examples above, that actually works and reads pretty
well.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs