Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Rational numbers (fractions) for node.js
JavaScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
test
.gitignore
.travis.yml
CHANGELOG.md
LICENSE
README.md
index.js
package.json

README.md

ratio - ℚ in JS build status

Sometimes you want to handle a ratio as its own entity. Yeah, you could downcast to decimal (though on floating point that's really a crime), or encode it as a string, but neither of those really cuts it.

So why not npm install ratio?

var ratio = require('ratio')
  , half = ratio(4,8)

half === 0.5 // true
half.toString() === '1/2' // true
ratio(0.02).toSring() === '1/50'

features

  • flexible instantiation:
    • from numerator and denominator
    • from string
    • from decimal number
  • nice rendering
    • valueOf() returns numeric form
    • toString() renders fractional form
  • automatically reduces fractions on creation
  • fraction arithmetic
    • addition with r.plus(x)
    • subtraction with r.minus(x)
    • multiplication with r.times(x)
    • division with r.div(x)
    • reciprocal with r.reciprocal()
    • negative with r.neg()

still to come

  • render unicode fractions?
Something went wrong with that request. Please try again.