I want to perform mathematical operations using the font-size information with the function below. I remove the rem
unit information of $font-size
using the sass build-in string functions, but then I cannot perform mathematical operations because the value type is string.
Is there a way to convert a string value type to numeric value type in sass?
@use "../abstracts/variables" as *;
@use "sass:math";
@function em($value, $font-size) {
$length: str-length(#{$font-size}); //sample-think about it font-size 1.125rem
$trim: ($length - 3);
$data: str-slice(#{$font-size}, 1, $trim);
$result: math.div($value, $data);
@return $result + "em";
}
It would probably be better to use a helper function to remove the unit of $font-size
such as this one.
@function stripUnit($number) {
@if meta.type-of($number) == "number" and not math.is-unitless($number) {
@return math.div($number, $number * 0 + 1);
}
@return $number;
}
@function em($value, $font-size) {
$data: strip-unit($font-size);
$result: math.div($value, $data);
@return $result + "em";
}