GithubHelp home page GithubHelp logo

rounded_percents's Introduction

Overview

Round percentages, ensuring that the results add up to 100%.

Installation

    gem "rounded_percents", git: 'https://github.com/ktopping/rounded_percents'

Usage

RoundedPercents extends Array so it responds to all the Array-like methods (e.g. [], .length, .sum, etc.)

    require 'rounded_percents'

    values = [1, 2, 3]
    RoundedPercents.new(values)
    # => [17, 33, 50]

    RoundedPercents.new(values, precision: 1)
    # => [16.7, 33.3, 50.0]

Algorithms

Specify algorithm as follows:

    RoundedPercents.new(values, algorithm: RoundedPercents::LAD)

LAD (Least Absolute Difference)

This is the default, described best here: https://stackoverflow.com/a/13483710 In short, round everything down, work out how far short of 100% the sum is, and assign this shortfall to prioritising those where it makes the "Least Absolute Difference". In practise, where precision=0, this means "in reverse order of decimal component".

LRD (Least Relative Difference)

Like the above, but tries to reassign the shortfall according to where it would make the least relative difference. Discussed here: https://stackoverflow.com/a/34959983

LRD_AVOID_ZERO (Least Relative Difference, Avoid Zero)

More of an experiment, but - like LRD BUT attempts to avoid rounding down to 0.

  RoundedPercents.new([99.5, 0.5], algorithm: RoundedPercents::LRD)
  # => [100, 0]
  RoundedPercents.new([99.5, 0.5], algorithm: RoundedPercents::LRD_AVOID_ZERO)
  # => [99, 1]

Other Algorithms (Not Implemented)

"Carry the error"

https://stackoverflow.com/a/13483486

"Thresholds"

Proposal, thresholds: [1, 5] to mean that:

  • All percentages that are greater that zero, but less than 1 should be displayed as "<1".
  • All percentages that are greater than 1, but less than 5 should display as "<5".

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.