SixArm.com » Ruby » Numeric#percent to calculate a percentage

Author

Joel Parker Henderson, [email protected]

Copyright

Copyright © 2006-2012 Joel Parker Henderson

License

See LICENSE.txt file

Calculate a number as percentage with rounding:

x=0.1234
x.percent => 12

Optional precision:

x.percent(nil) => 12 (default is to round to the closest integer)
x.percent(1) => 10
x.percent(2) => 12
x.percent(3) => 12.3
x.percent(3) => 12.34

Note that precision 1 and 2 are optimized for speed and to return integers; other precisions return floats.