math.calculate()

The math.calculate() function performs value and operand calculation by using a given operator + addition, - subtraction, * multiplication, / division of string type.

// Sass.
@use 'sass:list';
@use 'sass:meta';

// The `math.calculate()` function.
@function calculate($operations...) {
  @each $value, $operator,
    $operand in if(list.length($operations) == 3, ($operations), $operations)
  {
    @if $value and $operand {
      @if $operator == '+' {
        @return calc($value + $operand);
      } @else if $operator == '-' {
        @return calc($value - $operand);
      } @else if $operator == '*' {
        @return calc($value * $operand);
      } @else if $operator == '/' {
        @return calc($value / $operand);
      }
    }
  }
  @return null;
}
https://github.com/angular-package/sass/blob/main/math/_math.calculate.function.scss

Parameters

$operations...

Arbitrary values in order (value, operator, operand) to perform calculation.

Return

The return value is calculated number or CSS function calc() with a specified calculation.

Examples

Last updated

Was this helpful?