Skip to content

Instantly share code, notes, and snippets.

@KittyGiraudel
Last active August 29, 2015 13:57
Show Gist options
  • Save KittyGiraudel/9832501 to your computer and use it in GitHub Desktop.
Save KittyGiraudel/9832501 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// libsass (v3.1.0-beta)
// ----
/// Power function
/// @param {Number} $x
/// @param {Number} $n
/// @return {Number}
@function pow($x, $n) {
$ret: 1;
@if $n >= 0 {
@for $i from 1 through $n {
$ret: $ret * $x;
}
} @else {
@for $i from $n to 0 {
$ret: $ret / $x;
}
}
@return $ret;
}
/// toFixed() function in Sass
/// @param {Number} $float - number to format
/// @param {Number} $digits (2) - number of digits to leave
/// @return {Number}
@function to-fixed($float, $digits: 2) {
$sass-precision: 5;
@if $digits > $sass-precision {
@warn "Sass sets default precision to #{$sass-precision} digits, and there is no way to change that for now."
+ "The returned number will have #{$sass-precision} digits, even if you asked for `#{$digits}`."
+ "See https://github.com/sass/sass/issues/1122 for further informations.";
}
$pow: pow(10, $digits);
@return round($float * $pow) / $pow;
}
/// Alias function to match JavaScript name
/// @alias to-fixed
@function toFixed($args...) {
@return to-fixed($args);
}
/// Alias function to allow the use of precision()
/// @alias to-fixed
@function precision($args...) {
@return to-fixed($args);
}
// Test case
sass {
$number: 3.14159265359;
to-fixed: to-fixed($number, 0);
to-fixed: to-fixed($number, 1);
to-fixed: to-fixed($number); // Default $digits is 2
to-fixed: to-fixed($number, 3);
to-fixed: to-fixed($number, 4);
to-fixed: to-fixed($number, 5);
}
sass {
to-fixed: 3.14;
to-fixed: 3.1;
to-fixed: 3.14;
to-fixed: 3.142;
to-fixed: 3.1416;
to-fixed: 3.14159; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment