Last active
December 2, 2016 11:04
-
-
Save allejo/9246d143aefd14fd1cec66c3707eba08 to your computer and use it in GitHub Desktop.
Do division in Liquid templates with support for decimals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% capture whitespace %} | |
{% assign wholeNum = include.number | divided_by: include.divsor %} | |
{% assign remainder = wholeNum | times: include.divsor | minus: include.number | times: -1 %} | |
{% capture output %}{{ wholeNum }}.{% endcapture %} | |
{% for i in (1..include.decimals) %} | |
{% assign newNumer = remainder | times: 10 %} | |
{% assign workspace = newNumer | divided_by: include.divsor %} | |
{% assign remainder = workspace | times: include.divsor | minus: newNumer | times: -1 %} | |
{% if forloop.last %} | |
{% assign newRound = remainder | times: 10 %} | |
{% assign roundWorkspace = newRound | divided_by: include.divsor %} | |
{% if roundWorkspace >= 5 %} | |
{% assign workspace = workspace | plus: 1 %} | |
{% endif %} | |
{% endif %} | |
{% capture output %}{{ output }}{{ workspace }}{% endcapture %} | |
{% endfor %} | |
{% endcapture %}{{ output }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3.142857143 | |
Can only go from ints to decimals, not decimals to decimals | |
{% include division.html number=22 divsor=7 decimals=4 %} | |
This'll give you a decimal | |
{{ 22.5 | divided_by: 7 }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment