Created
February 18, 2019 21:32
-
-
Save danielbachhuber/ed56cde1eabd6677e34e23cb56f591b6 to your computer and use it in GitHub Desktop.
Convert a number to a fraction in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts numbers to fractions: | |
* - 1.25 to 1 1/4 | |
* - 2 to 2 | |
*/ | |
var numberToFraction = function( amount ) { | |
// This is a whole number and doesn't need modification. | |
if ( parseFloat( amount ) === parseInt( amount ) ) { | |
return amount; | |
} | |
// Next 12 lines are cribbed from https://stackoverflow.com/a/23575406. | |
var gcd = function(a, b) { | |
if (b < 0.0000001) { | |
return a; | |
} | |
return gcd(b, Math.floor(a % b)); | |
}; | |
var len = amount.toString().length - 2; | |
var denominator = Math.pow(10, len); | |
var numerator = amount * denominator; | |
var divisor = gcd(numerator, denominator); | |
numerator /= divisor; | |
denominator /= divisor; | |
var base = 0; | |
// In a scenario like 3/2, convert to 1 1/2 | |
// by pulling out the base number and reducing the numerator. | |
if ( numerator > denominator ) { | |
base = Math.floor( numerator / denominator ); | |
numerator -= base * denominator; | |
} | |
amount = Math.floor(numerator) + '/' + Math.floor(denominator); | |
if ( base ) { | |
amount = base + ' ' + amount; | |
} | |
return amount; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That is DOPE bro ! thanks