Question:
There is a string "14px", how to convert it to a number in SCSS?
For example, for a string representation of a value, there is a function inspect($value)
, in order to find out the units associated with a number, there is unit($number)
. But what if type-of(14рх)
returns string
, but you need namber
.
I explain: there is such a code that looks to see if the value of the list has units of measurement, and if not, then appends
@function chekList($list) {
$tmp: ();
@each $key in $list {
@if(unitless($key)) {
$key: inspect($key);
$key: str-insert($key, "px", str-length($key) + 1);
}
$tmp: append($tmp, $key);
}
@return $tmp;
}
Now to convert the resulting string values into numeric ….
Or advise how to add units to the number, if there are none?
PS Below I gave an answer on how to add units of measurement if they are not. But still interested in how you can convert a string to a number.
Let's say 10 + "px" at the output will give the string "10px", but you need it to be a number.
Answer:
It might come in handy for someone. I did not find a function to convert a string to a number, but in order to add units of measurement to a number, if there are none, you can simply multiply this number by one with the desired units of measurement.
@if(unitless($key)) {
$key: $key * 1px;
}