readysteady / percentage Goto Github PK
View Code? Open in Web Editor NEWRuby gem for working with percentages
Home Page: https://rubygems.org/gems/percentage
License: Other
Ruby gem for working with percentages
Home Page: https://rubygems.org/gems/percentage
License: Other
I am using this gem to calculate the percentage but getting error sometimes when the min value is o, for example
percentage = Percentage.change(0, 1)
in that case I am getting error like
ZeroDivisionError: divided by 0
from /usr/local/bundle/gems/percentage-1.4.1/lib/percentage.rb:118:in `/'
if you can manage it in some good manner so that can be easy to handle from the developers end.
Adding .truncate(2)
after every call to Percentage.new
when displaying the information in a table is rather tedious.
What are your thoughts on having an option to set truncation for future calls?
Something like the following would be nice and easy to implement.
Percentage.default_truncation = 2
Percentage.new(Rational(1,3)).to_s
# => 0.33%
When working with rails you could then create an initialiser app/config/initializers/percentage.rb
to set this globally.
# app/config/initializers/percentage.rb
Percentage.default_truncation = 2
Thank you for the helpful library! I was recently investigating using it in place of our own internal implementation.
One issue that I ran into is that Percentage
instances can't be used directly in arithmetic with Ruby Money
, because they implement type checking. It took me a couple tries to get the behavior I wanted:
Money.new(10) * Percentage.new(10) # expecting => #<Money 1>
=> TypeError: Can't multiply a Money by a Percentage's value
then
Money.new(10) * Percentage.new(10).to_f # expecting => #<Money 1>
=> #<Money 100>
finally
Money.new(10) * Percentage.new(10).to_r
=> #<Money 1>
So I suppose I have a couple questions:
Percentage
a subclass of Numeric
since it does represent a numeric value? I believe you already have the required interfaces implemented (arithmetic and coerce
).Percentage
is not the percentage in decimal form. Why is that the case?Thank you again for taking the time to read this, and thank you for maintaining a useful library โค๏ธ๐๐๐๐
I'd like to be able to use decimals this way. However, I note that the library treats a decimal value between 0 and 1 as a percentage of 1. I'd prefer to keep it as a scale of 0 to 100. When I say "0.5 percent of 88", I don't mean 50 percent.
What do you think
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.