Giter Site home page Giter Site logo

percentage's People

Contributors

iamvery avatar mikeymicrophone avatar orien avatar timcraft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

percentage's Issues

Error on `change` method ZeroDivisionError: divided by 0

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.

Thoughts on Class variable for default truncation

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

Should Percentage be Numeric?

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:

  1. Have you considered making Percentage a subclass of Numeric since it does represent a numeric value? I believe you already have the required interfaces implemented (arithmetic and coerce).
  2. I was surprised that the floating point conversion of a 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 โค๏ธ๐Ÿ’™๐Ÿ’š๐Ÿ’›๐Ÿ’œ

BigDecimal#percent_of

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.