Skip to content

Instantly share code, notes, and snippets.

@stnvh
Last active August 29, 2015 14:06
Show Gist options
  • Save stnvh/d579ac46b365145a6942 to your computer and use it in GitHub Desktop.
Save stnvh/d579ac46b365145a6942 to your computer and use it in GitHub Desktop.
Decimal to simplified fraction in SCSS
/* 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