From bda77bc93462f80560efc9d07041ee1647a9d32a Mon Sep 17 00:00:00 2001 From: Adam Hollett Date: Wed, 15 Jun 2016 09:56:06 -0400 Subject: [PATCH 1/3] Better explanation of divided_by filter --- filters/divided_by.md | 98 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 79 insertions(+), 19 deletions(-) diff --git a/filters/divided_by.md b/filters/divided_by.md index caeef2f2..a6bc2faa 100644 --- a/filters/divided_by.md +++ b/filters/divided_by.md @@ -4,40 +4,100 @@ title: divided_by Divides a number by the specified number. -The result is rounded down to the nearest integer (that is, the [floor]({{ "/filters/floor" | prepend: site.baseurl }})). +The result is rounded down to the nearest integer (that is, the [floor]({{ "/filters/floor" | prepend: site.baseurl }})) if the divisor is an integer.

Input

-```liquid -{% raw %} -{{ 4 | divided_by: 2 }} -{% endraw %} -``` - -

Output

-```text -{{ 4 | divided_by: 2 }} -``` - -

Input

-```liquid {% raw %} +``` liquid {{ 16 | divided_by: 4 }} -{% endraw %} ``` +{% endraw %}

Output

-```text +``` text {{ 16 | divided_by: 4 }} ```

Input

-```liquid {% raw %} +``` liquid {{ 5 | divided_by: 3 }} -{% endraw %} ``` +{% endraw %}

Output

-```text +``` text {{ 5 | divided_by: 3 }} ``` + +### Controlling rounding + +`divided_by` produces a result of the same type as the divisor — that is, if you divide by an integer, the result will be an integer. If you divide by a float (a number with a decimal in it), the result will be a float. + +For example, here the divisor is an integer: + +

Input

+{% raw %} +``` liquid +{{ 20 | divided_by: 7 }} +``` +{% endraw %} + +

Output

+``` text +{{ 20 | divided_by: 7 }} +``` + +Here it is a float: + +

Input

+{% raw %} +``` liquid +{{ 20 | divided_by: 7.0 }} +``` +{% endraw %} + +

Output

+``` text +{{ 20 | divided_by: 7.0 }} +``` + +### Changing variable types + +You might want to use a variable as a divisor, in which case you can't simply add `.0` to convert it to a float. In these cases, you can `assign` a version of your variable converted to a float using the `times` filter: + +In this example, we're dividing by a variable that contains an integer, so we get an integer: + +

Input

+{% raw %} +``` liquid +{% assign my_integer = 7 %} +{{ 20 | divided_by: my_integer }} +``` +{% endraw %} + + +

Output

+``` text +{% assign my_integer = 7 %} +{{ 20 | divided_by: my_integer }} +``` + +Here, we [multiply]({{ site.baseurl}}/filters/times) the variable by `1.0` to get a float, then divide by the float instead: + +

Input

+{% raw %} +``` liquid +{% assign my_integer = 7 %} +{% assign my_float = my_integer | times: 1.0 %} +{{ 20 | divided_by: my_float }} +``` +{% endraw %} + + +

Output

+``` text +{% assign my_integer = 7 %} +{% assign my_float = my_integer | times: 1.0 %} +{{ 20 | divided_by: my_float }} +``` From 1e093f9cb6cb3b36dfca637597a3efce3e63202e Mon Sep 17 00:00:00 2001 From: Adam Hollett Date: Wed, 15 Jun 2016 10:00:24 -0400 Subject: [PATCH 2/3] Remove errant colon --- filters/divided_by.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filters/divided_by.md b/filters/divided_by.md index a6bc2faa..7cedea3b 100644 --- a/filters/divided_by.md +++ b/filters/divided_by.md @@ -64,7 +64,7 @@ Here it is a float: ### Changing variable types -You might want to use a variable as a divisor, in which case you can't simply add `.0` to convert it to a float. In these cases, you can `assign` a version of your variable converted to a float using the `times` filter: +You might want to use a variable as a divisor, in which case you can't simply add `.0` to convert it to a float. In these cases, you can `assign` a version of your variable converted to a float using the `times` filter. In this example, we're dividing by a variable that contains an integer, so we get an integer: From 20562fb32ae74137c7ea1734c966214d923cd9cd Mon Sep 17 00:00:00 2001 From: Adam Hollett Date: Wed, 15 Jun 2016 10:02:56 -0400 Subject: [PATCH 3/3] Markdown linting --- filters/divided_by.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/filters/divided_by.md b/filters/divided_by.md index 7cedea3b..fc6abc3c 100644 --- a/filters/divided_by.md +++ b/filters/divided_by.md @@ -4,7 +4,7 @@ title: divided_by Divides a number by the specified number. -The result is rounded down to the nearest integer (that is, the [floor]({{ "/filters/floor" | prepend: site.baseurl }})) if the divisor is an integer. +The result is rounded down to the nearest integer (that is, the [floor]({{ site.baseurl }}/filters/floor)) if the divisor is an integer.

Input

{% raw %} @@ -76,7 +76,6 @@ In this example, we're dividing by a variable that contains an integer, so we ge ``` {% endraw %} -

Output

``` text {% assign my_integer = 7 %} @@ -94,7 +93,6 @@ Here, we [multiply]({{ site.baseurl}}/filters/times) the variable by `1.0` to ge ``` {% endraw %} -

Output

``` text {% assign my_integer = 7 %}