irb(main):011:0> format("%.2f",125)
=> "125.00"
irb(main):012:0> format("%.2f",125%)
SyntaxError: compile error
(irb):12: syntax error, unexpected ')'
···
--
Posted via http://www.ruby-forum.com/.
irb(main):011:0> format("%.2f",125)
=> "125.00"
irb(main):012:0> format("%.2f",125%)
SyntaxError: compile error
(irb):12: syntax error, unexpected ')'
--
Posted via http://www.ruby-forum.com/.
From a mathematical aspect the % sign means 1/100 so 125 % equals
1.25.
Pen Ttt 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
Pen,
On Saturday 21 August 2010, Pen Ttt <myocean135@yahoo.cn> 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
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 ![]()
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 <eveith@wwweb-library.net> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Pen,
On Saturday 21 August 2010, Pen Ttt <myocean135@yahoo.cn> 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-----
?> class Fixnum
def percent
the appropriate place to put that is Integer, not Fixnum.
On Aug 22, 2010, at 13:32 , Dick Davies wrote:
% ruby -e 'p 100.class.superclass'
Integer
?> class Fixnum
def percent
the appropriate place to put that is Integer, not Fixnum.
% ruby -e 'p 100.class.superclass'
Integer
Or even Numeric:
1.class.ancestors
=> [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]
class Numeric
def percent
self.to_f / 100.0
end
end
=> nil
5.percent
=> 0.05
125.percent
=> 1.25
14.7.percent
=> 0.147
(99+"44/100".to_r).percent
=> 0.9944
-Rob
Rob Biedenharn
Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab@GaslightSoftware.com http://GaslightSoftware.com/
On Aug 22, 2010, at 7:59 PM, Ryan Davis wrote:
On Aug 22, 2010, at 13:32 , Dick Davies wrote:
Ah, good catch both, thanks.
On Mon, Aug 23, 2010 at 2:02 AM, Rob Biedenharn <Rob@agileconsultingllc.com> wrote:
On Aug 22, 2010, at 7:59 PM, Ryan Davis wrote:
On Aug 22, 2010, at 13:32 , Dick Davies wrote:
?> class Fixnum
def percent
the appropriate place to put that is Integer, not Fixnum.
% ruby -e 'p 100.class.superclass'
IntegerOr even Numeric:
> 1.class.ancestors
=> [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]
> class Numeric
> def percent
> self.to_f / 100.0
> end
> end
=> nil
> 5.percent
=> 0.05
> 125.percent
=> 1.25
> 14.7.percent
=> 0.147
> (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.
On Aug 22, 2010, at 18:02 , Rob Biedenharn wrote:
Or even Numeric:
> 1.class.ancestors
=> [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]
> class Numeric
> def percent
> self.to_f / 100.0
> end
> end
=> nil
> 5.percent
=> 0.05
> 125.percent
=> 1.25
> 14.7.percent
=> 0.147
> (99+"44/100".to_r).percent
=> 0.9944
> Or even Numeric:
> > class Numeric
> > def percent
> > self.to_f / 100.0
> > end
> > 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 Mon, Aug 23, 2010 at 8:22 AM, Ryan Davis <ryand-ruby@zenspider.com>wrote:
On Aug 22, 2010, at 18:02 , Rob Biedenharn wrote: