Giter Club home page Giter Club logo

humanize_fraction's Introduction

humanize_fraction CircleCI Gem Version

Rubygem to convert fraction to words, like 1 ⅓ => one and a third. Examples:

"1/8".humanize_fraction
#=> "one eighth"

"1/8".humanize_fraction(shorthand: true)
#=> "an eighth"

"-2 3/4".humanize_fraction
#=> "negative two and three fourths"

"-2 3/4".humanize_fraction(quarter: true)
#=> "negative two and three quarters"

"222/333".humanize_fraction
#=> "two hundred and twenty-two three hundred thirty-thirds"

"1/1000000000000000000000000000".humanize_fraction(shorthand: true)
#=> "an octillionth"

If you don't want to monkey patch String, you can also use the Humanizer class directly:

# From a string:
fraction = HumanizeFraction::Humanizer.from_string("2 1/4")

# From integers:
fraction = HumanizeFraction::Humanizer.new(
  whole_part: 2,
  numerator: 1,
  denominator: 4,
)

fraction.to_s
#=> "two and one fourth"

fraction.to_s(shorthand: true, quarter: true)
#=> "two and a quarter"

Installation

Add this line to your application's Gemfile and execute bundle to install:

gem 'humanize_fraction'

If you wish to use the monkey-patched String#humanize_fraction, add the following into an initializer/config:

require 'humanize_fraction'
String.include(CoreExtensions::String::HumanizeFraction)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

humanize_fraction's People

Contributors

6 avatar

Stargazers

Pulkit Kathuria avatar

Watchers

James Cloos avatar  avatar

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.