Skip to content

Instantly share code, notes, and snippets.

@7studio
Last active December 25, 2015 14:09
Show Gist options
  • Save 7studio/6989350 to your computer and use it in GitHub Desktop.
Save 7studio/6989350 to your computer and use it in GitHub Desktop.
@function adjust_decimal_precision($f, $v, $p: 0) {
$n : if(0>=$p, 1, 10);
@while ($p > 1) {
$n: 10*10;
$p: $p - 1;
}
@if 'percentage' == $f { @return round(percentage($v) * $n) / $n; }
@else if 'round' == $f { @return round($v * $n) / $n; }
@else if 'ceil' == $f { @return ceil($v * $n) / $n; }
@else if 'floor' == $f { @return floor($v * $n) / $n; }
@return $v;
}
// sass test.scss:test.css --precision 3
@import 'adjust-decimal-precision';
@debug adjust_decimal_precision(percentage, (1/12), 2); // 8.33%
@debug adjust_decimal_precision(round, 8.3333%); // 8%
@debug adjust_decimal_precision(ceil, 8.3333%, 2); // 8.34%
@debug adjust_decimal_precision(null, 8.3333%, 1); // 8.333%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment