sass-site/source/documentation/functions/math.html.erb

206 lines
5.2 KiB
Plaintext
Raw Normal View History

2018-09-01 22:35:20 +02:00
---
title: Number Functions
---
<% function 'abs($number)', returns: 'number' do %>
Returns the [absolute value][] of `$number`. If `$number` is negative, this
returns `-$number`, and if `$number` is positive, it returns `$number` as-is.
2018-09-01 22:35:20 +02:00
[absolute value]: https://en.wikipedia.org/wiki/Absolute_value
2018-09-01 22:35:20 +02:00
<% example(autogen_css: false) do %>
@debug abs(10px); // 10px
@debug abs(-10px); // 10px
===
@debug abs(10px) // 10px
@debug abs(-10px) // 10px
<% end %>
2018-09-01 22:35:20 +02:00
<% end %>
<% function 'ceil($number)', returns: 'number' do %>
Rounds `$number` up to the next highest whole number.
<% example(autogen_css: false) do %>
@debug ceil(4); // 4
@debug ceil(4.2); // 5
@debug ceil(4.9); // 5
===
@debug ceil(4) // 4
@debug ceil(4.2) // 5
@debug ceil(4.9) // 5
<% end %>
2018-09-01 22:35:20 +02:00
<% end %>
2018-10-25 00:01:43 +02:00
<% function 'comparable($number1, $number2)', returns: 'boolean' do %>
Returns whether `$number1` and `$number2` have compatible units.
If this returns `true`, `$number1` and `$number2` can safely be [added][],
[subtracted][], and [compared][]. Otherwise, doing so will produce errors.
[added]: ../operators/numeric
[subtracted]: ../operators/numeric
[compared]: ../operators/relational
<% example(autogen_css: false) do %>
@debug comparable(2px, 1px); // true
@debug comparable(100px, 3em); // false
@debug comparable(10cm, 3mm); // true
===
@debug comparable(2px, 1px) // true
@debug comparable(100px, 3em) // false
@debug comparable(10cm, 3mm) // true
<% end %>
<% done %>
2018-09-01 22:35:20 +02:00
<% function 'floor($number)', returns: 'number' do %>
Rounds `$number` down to the next lowest whole number.
<% example(autogen_css: false) do %>
@debug floor(4); // 4
@debug floor(4.2); // 4
@debug floor(4.9); // 4
===
@debug floor(4) // 4
@debug floor(4.2) // 4
@debug floor(4.9) // 4
<% end %>
2018-09-01 22:35:20 +02:00
<% end %>
<% function 'max($number...)', returns: 'number' do %>
Returns the highest of one or more numbers. A list of numbers can be passed
[using `...`][].
2018-09-01 22:35:20 +02:00
[using `...`]: ../at-rules/function#passing-arbitrary-arguments
2018-09-01 22:35:20 +02:00
<% example(autogen_css: false) do %>
@debug max(1px, 4px); // 4px
2018-09-01 22:35:20 +02:00
$widths: 50px, 30px, 100px;
@debug max($widths...); // 100px
===
@debug max(1px, 4px) // 4px
2018-09-01 22:35:20 +02:00
$widths: 50px, 30px, 100px
@debug max($widths...) // 100px
<% end %>
2018-09-01 22:35:20 +02:00
<% end %>
<% function 'min($number...)', returns: 'number' do %>
Returns the lowest of one or more numbers. A list of numbers can be passed
[using `...`][].
2018-09-01 22:35:20 +02:00
[using `...`]: ../at-rules/function#passing-arbitrary-arguments
2018-09-01 22:35:20 +02:00
<% example(autogen_css: false) do %>
@debug min(1px, 4px); // 1px
2018-09-01 22:35:20 +02:00
$widths: 50px, 30px, 100px;
@debug min($widths...); // 30px
===
@debug min(1px, 4px) // 1px
2018-09-01 22:35:20 +02:00
$widths: 50px, 30px, 100px
@debug min($widths...) // 30px
<% end %>
2018-09-01 22:35:20 +02:00
<% end %>
<% function 'percentage($number)', returns: 'number' do %>
Converts a unitless `$number` (usually a decimal between 0 and 1) to a
percentage.
2018-09-01 22:35:20 +02:00
<% fun_fact do %>
This function is identical to `$number * 100%`.
<% end %>
2018-09-01 22:35:20 +02:00
<% example(autogen_css: false) do %>
@debug percentage(0.2); // 20%
@debug percentage(100px / 50px); // 200%
===
@debug percentage(0.2) // 20%
@debug percentage(100px / 50px) // 200%
<% end %>
2018-09-01 22:35:20 +02:00
<% end %>
<% function 'random($limit: null)', returns: 'number' do %>
If `$limit` is [`null`][], returns a random decimal number between 0 and 1.
2018-09-01 22:35:20 +02:00
[`null`]: ../values/null
2018-09-01 22:35:20 +02:00
<% example(autogen_css: false) do %>
@debug random(); // 0.2821251858
@debug random(); // 0.6221325814
===
@debug random() // 0.2821251858
@debug random() // 0.6221325814
<% end %>
2018-09-01 22:35:20 +02:00
* * *
2018-09-01 22:35:20 +02:00
If `$limit` is a number greater than or equal to 1, returns a random whole
number between 1 and `$limit`.
2018-09-01 22:35:20 +02:00
<% example(autogen_css: false) do %>
@debug random(10); // 4
@debug random(10000); // 5373
===
@debug random(10) // 4
@debug random(10000) // 5373
<% end %>
2018-09-01 22:35:20 +02:00
<% end %>
<% function 'round($number)', returns: 'number' do %>
Rounds `$number` to the nearest whole number.
<% example(autogen_css: false) do %>
@debug round(4); // 4
@debug round(4.2); // 4
@debug round(4.9); // 5
===
@debug round(4) // 4
@debug round(4.2) // 4
@debug round(4.9) // 5
<% end %>
2018-09-01 22:35:20 +02:00
<% end %>
2018-10-25 00:01:43 +02:00
<% function 'unit($number)', returns: 'quoted string' do %>
Returns a string representation of `$number`'s units.
<% heads_up do %>
This function is intended for debugging; its output format is not guaranteed
to be consistent across Sass versions or implementations.
<% end %>
<% example(autogen_css: false) do %>
@debug unit(100); // ""
@debug unit(100px); // "px"
@debug unit(5px * 10px); // "px*px"
@debug unit(5px / 1s); // "px/s"
===
@debug unit(100) // ""
@debug unit(100px) // "px"
@debug unit(5px * 10px) // "px*px"
@debug unit(5px / 1s) // "px/s"
<% end %>
<% end %>
<% function 'unitless($number)', returns: 'boolean' do %>
Returns whether `$number` has no units.
<% example(autogen_css: false) do %>
@debug unitless(100); // true
@debug unitless(100px); // false
===
@debug unitless(100) // true
@debug unitless(100px) // false
<% end %>
<% end %>