Last active
August 29, 2015 14:06
-
-
Save stnvh/d579ac46b365145a6942 to your computer and use it in GitHub Desktop.
Decimal to simplified fraction in SCSS
This file contains 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
/* Decimal to simplified fraction (using the Euclidean algorithm) | |
---------------------------------------------------------------------------------- */ | |
@function fraction($num) { | |
$top: $num * 100; | |
$bottom: 100; | |
$return: ''; | |
$a: abs($top); $b: abs($bottom); | |
@if($a < $b) { | |
$old-b: $b; | |
$b: $a; | |
$a: $old-b; | |
} | |
@if($b == 0) { | |
$return: $a; | |
} @else { | |
$r: $a % $b; | |
@while($r > 0) { | |
$a: $b; | |
$b: $r; | |
$r: $a % $b; | |
} | |
$return: $b; | |
} | |
@return ($top / $return) + '/' + ($bottom / $return); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment