Skip to content

Instantly share code, notes, and snippets.

@envoytravis
Created April 8, 2015 19:53
Show Gist options
  • Save envoytravis/7c02246efabfc65e7fab to your computer and use it in GitHub Desktop.
Save envoytravis/7c02246efabfc65e7fab to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.4.12)
// Compass (v1.0.3)
// ----
@function _length($number, $unit) {
$strings: 'px' 'cm' 'mm' '%' 'ch' 'pica' 'in' 'em' 'rem' 'pt' 'pc' 'ex' 'vw' 'vh' 'vmin' 'vmax';
$units: 1px 1cm 1mm 1% 1ch 1pica 1in 1em 1rem 1pt 1pc 1ex 1vw 1vh 1vmin 1vmax;
$index: index($strings, $unit);
@if not $index {
@warn "Unknown unit `#{$unit}`.";
@return false;
}
@return $number * nth($units, $index);
}
@function number($string) {
// Matrices
$strings: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
$numbers: 0 1 2 3 4 5 6 7 8 9;
// Result
$result: 0;
$divider: 0;
$minus: false;
// Looping through all characters
@for $i from 1 through str-length($string) {
$character: str-slice($string, $i, $i);
$index: index($strings, $character);
@if $character == '-' {
$minus: true;
}
@else if $character == '.' {
$divider: 1;
}
@else {
@if not $index {
$result: if($minus, $result * -1, $result);
@return _length($result, str-slice($string, $i));
}
$number: nth($numbers, $index);
@if $divider == 0 {
$result: $result * 10;
}
@else {
// Move the decimal dot to the left
$divider: $divider * 10;
$number: $number / $divider;
}
$result: $result + $number;
}
}
@return if($minus, $result * -1, $result);
}
@function roundBy($numbers, $amount) {
$roundBy: null;
// loop through and find the amount to round by
@for $i from 1 through $amount+1 {
$rounder: 1;
@if $i > 1 {
$rounder: 0;
}
$roundBy: $roundBy#{$rounder};
}
// convert string to number
$roundBy: number($roundBy);
$numbers: $numbers * $roundBy;
@return round($numbers)/$roundBy;
}
.test {
test: roundBy(10.3453416, 2);
}
.test {
test: 10.35;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment