Using Sass, how to convert a percentage to a decimal?
here's @Rhysyngsun ruby script as a scss function:
@function decimal($v) {
@if (type_of($v) != number OR unit($v) != "%") {
@error "decimal: `#{$v}` is not a percent";
}
@return $v / 100%;
}
While SASS doesn't provide this as a built-in function you could certainly add a custom function to perform the conversion. Taking a look at the source for percentage(), I took a stab at what a decimal() function would look like:
def decimal(value)
unless value.is_a?(Sass::Script::Number) && value.unit_str == "%"
raise ArgumentError.new("#{value.inspect} is not a percent")
end
Sass::Script::Number.new(value.value / 100.0)
end
You can use Sass math to convert a percentage to a decimal value by dividing the percentage by 100%
.
When you divide a percentage by a percentage, the result is a decimal.
Sass code:
$percent: 44%
.foo
opacity: $percent / 100%
Compiled to CSS:
.foo { opacity: 0.44; }