fraction-calculator
This is a library to calculate fraction. This library :
- can calculate floating number precisely
- can calculate(plus, minus, times, div, mod...) a fraction
- can power or sqrt a fraction.
- can convert a decimal to a fraction and convert it back
- can convert a recurring decimal to a fraction and convert it back
- supports IE
- has 100% UT coverage
- has 0 dependencies
Usage
Use it in ES6 or Node.js
Install:
npm install fraction-calculator --save
Then:
// ES6 import; // Node.jsconst fc = ; // Use; // 467288576/3906984375
Use it in modern browsers
This library has used some ES6 features, if you only target modern browsers, you can use ./dist/fraction-calculator.min.js
, this file is only 7kb. This script will set a varable fc
in the window, you can call it directly.
Use it in old browsers
If you also target some old browsers, like IE, you can use ./dist/fraction-calculator.polyfill.min.js
, it is built with babel-polyfill
。
Features
Calculating floating number precisely
01 + 02; // JS: 0.30000000000000004; // 0.3
Calculating fraction
; // "97/150"
Converting a decimal to a fraction and convert it back
; // 739/5000 ; // 0.1478
Converting a recurring decimal to a fraction and convert it back
; // 60733/49995; // 1.2(1478)
API
All API
When you use this library, you shouldn't use new
, just fc()
. Let's take a look at all API:
// constractor // calculation api // compare api // display api // others // static apifcfc // configfcDISABLE_REDUCE
constractor
Fraction calculator constractor supports many kinds of parameter, but if you pass an invalid parameter, you will get an error:
; // Error: Unsupported parameter [object Object]
String
; // It is 1/2
If you pass 0 or NaN
as denominator
, you will get an error:
; // Error: Denominator can't be 0 or NaN
Sting with whole part
; // It is 3/2
Number
; // It is still 3/2
If you pass a NaN
or Infinity
, you will get an error:
; // Error: Unsupported number NaN or Infinity
Two numbers
; // It is still 3/2
Recurring decimal
You can use ''
or()
to mark recurring part. Please note if you input a long decimals(over 15 numbers), you may only get a closed fraction, not precise one.
; // 419/3330; // 419/3330
Another instance
; // 419/3330
Calculation API
All calculation api are chainable, all return value is this
. It means you can call it like this:
; // 1
plus, minus, times, div,mod
You can know what they are doing from their names. They can support one parameter in all kinds that constractor supports. mod
will get the same result as JS %
.
; // 49/50; // 1/50; // 6/25; // 25/24 ; // Result: -14/3 . The same as (-29/3) % 5; // Result: 14/3 . The same as (29/3) % -5
pow(n)
pow
can only support a number
parameter, it is Math.pow(numerator, n)/Math.pow(denominator, n)
, if denominator
reached Infinity, fraction will be reset to 0. If numerator
reached Infinity
, it will throw an error:
; // Error: Numerator reached Infinity
sqrt()
It is pow('1/2')
.
If fraction is negative, you call sqrt
, you will get a error:
; // Error: Sqrt number cannot less than 0
abs()
Get the absolute value.
; // "1/2"
neg()
Get the negation.
; // "1/2" ; // "-1/2"
inverse()
Get the inverse.
; // "2"
ceil()
Based on Math.ceil()
.
floor()
Based on Math.floor()
.
round()
Based on Math.round()
.
Compare API
There are 3 compare API,equals
,greaterThan
,lessThan
. They can support one parameter in all kinds that constractor supports. Their return valus are boolean.
; // true; // true; // false
Display API
toFraction(withWholePart = false)
This API will show fraction in string, but if it is an integer, it will only show integer in string.This api has an optional parameterwithWholePart
, default is false
. You can set it to true
to show whole part.
; // "5"; // "11/2"; // "5 1/2"
toFixed(n)
Based on js toFixed
:
; // 2.57
toNumber()
This will show all decimals JS can handle:
; // 2.567867867867868
toRecurringDecimal()
This API will show fraction in recurring decimal, recurring part will marked with ()
. Please note recurring part can be very long, the search can be very slow if it is. So this library will only search first 3000 decimals, If still not find a pattern, it will output the whole 3000 numbers, like "0.1234567890.......".
; // "0.(2)"; // "0.269(736842105263157894)"
Static API
fc.gcd(a, b)
This API will return GCD(greatest common divisor) of a and b.
fc; // 9
But if a or b is NaN or Infinity, it will throw an error:
fc; // Error: Invalid Parameter
fc.lcm(a,b)
This API will return LCM(Lowest Common Multiple) of a and b.
fc; // 54
But if a or b is NaN or Infinity, it will throw an error:
fc; // Error: Invalid Parameter
Other API
clone()
This API will return a copy of current instance:
let fc1 = ;let fc2 = fc1; fc2; // "1/6"fc1; // "1/2"
Config
This library can work well without any config. But there is still a global config, you can disable reducing.
let fc1 = ; fc1; // 5/9 fcDISABLE_REDUCE = true; fc1; // 25/45 fcDISABLE_REDUCE = false; fc1; // 5/9
If you disable reducing, then call pow
or sqrt
, you may get decimals in numerator and denominator:
fcDISABLE_REDUCE = true; ; // "11.180339887498949/5.196152422706632"