【问题标题】:Liquid maths: calculating a percent discount in the order notification email template流动数学:计算订单通知电子邮件模板中的百分比折扣
【发布时间】:2015-12-02 13:12:11
【问题描述】:

为什么返回 0?

{% if discounts %}
  Discount
  {{ discounts_amount | divided_by: subtotal_price | times: 100 }}%
{% endif %}

我也尝试过适应整数数学,但没有运气。

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    语法错误。试试这个

    {% if discounts %}
      Discount {{ discounts.amount | divided_by: subtotal_price | times: 100 }}%
    {% endif %}
    

    【讨论】:

    • 请注意,如果discounts.amount 和subtotal_price 都是整数,则输出将是整数(截断,而不是四舍五入)。确保小数的一种方法是将times: 100 更改为times: 100.0 并将其放在divided_by 过滤器之前:{{ discounts.amount | times: 100.0 | divided_by: subtotal_price }}。
    • 提供的答案仍然是 0。感谢@Nick 的评论——我会看看这是否有效。
    • 我刚刚注意到您的问题中的discounts_amount 在@HymnZ 的答案中更改为discounts.amount,因此我的评论。这是导致它为零的原因吗?
    【解决方案2】:

    可能已经很久了,但由于浏览量很大,我还在回复。

    我们必须将整数转换为浮点数

    {% assign my_integer = 7 %}
    {% assign my_float = my_integer | times: 1.0 %}
    {{ 20 | divided_by: my_float }}
    

    https://shopify.github.io/liquid/filters/divided_by/

    【讨论】:

      猜你喜欢
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多