Shopify 液体脚本:将字符串转换为数字以显示为货币

Shopify 液体脚本:将字符串转换为数字以显示为货币,shopify,liquid,dotmailer,Shopify,Liquid,Dotmailer,我正在尝试在液体脚本中将浮点值显示为货币。它似乎只允许您对数字执行此操作,但我无法将字符串转换为数字(尽管下面给出了如下建议:) 我想要的是将字符串值(teststring)显示为money。所以我需要转换成一个数字,然后显示为货币 我也尝试过时报过滤器,例如 {% assign testStringAsNumeric = teststring | times: 1 %} 上面的测试显示为货币:{{testStringAsNumeric |货币:“GBP”} 但这会返回一个错误: test s

我正在尝试在液体脚本中将浮点值显示为货币。它似乎只允许您对数字执行此操作,但我无法将字符串转换为数字(尽管下面给出了如下建议:)

我想要的是将字符串值(teststring)显示为money。所以我需要转换成一个数字,然后显示为货币

我也尝试过时报过滤器,例如

{% assign testStringAsNumeric = teststring | times: 1 %}
上面的测试显示为货币:{{testStringAsNumeric |货币:“GBP”}

但这会返回一个错误:

test show above as money: System.Linq.Enumerable+d__112`1[System.String]

感谢您的帮助

如果您只有正数,可以使用将字符串转换为数字。这应该是这样的工作:

{% assign teststring = '202.2400' %}
teststring as money: {{ teststring | abs | money: "GBP" }}

当然,只有当这个数字已经是正数时,这个方法才正确。

所以我想解决这个问题中的一些问题

首先,当使用货币过滤器时,数字被解释为
美分
,例如

Input: {{ 2000 | money }}

Output: $20.00
因此,一旦格式正确,下面的行将显示
$2.47

test以货币形式显示上述内容:{{testStringAsNumeric |货币:“GBP”}

Second,设置
money
过滤器将使用哪种货币的方法是在shopfify admin内部,而不是在液滴内部作为参数。因此,一旦您在Shopify的管理员中设置了您的货币,您只需编写:

test显示为money:{{testStringAsNumeric | money}}

Third,使用
money
过滤器,只保留两个尾随零

Input: {{ 2040.002020203 | money }}

Ouput: {{ $20.40 }}
根据您的要求,您应该能够做到:

{% assign teststring =  '202.2400' %}
{% assign testnum = teststring | times: 100 %}
teststring as money {{ testnum | money }}

这是我在测试库中从您的液体代码中得到的输出:

teststring: 202.2400 
testnumeric: 202.24 
teststring as money: Liquid error: wrong number of arguments (2 for 1) 
testnumeric as money: Liquid error: wrong number of arguments (2 for 1) 
test convert teststring to numeric: 247.24 
test show above as money: Liquid error: wrong number of arguments (2 for 1)
| money:“GBP”
过滤器更改为简单的
|money
(Shopify通常如何使用该过滤器)可提供:

。。。这似乎是工作的预期。(是的,我的开发商店的shop.money_格式目前是
${{amount}!!
。这让我觉得好笑。)


您是否试图在应用程序的上下文中使用这些流动语句?如果是这样的话,该应用程序对液态代码的解释可能会有问题——但正如您所见,从纯Shopify的角度来看,您的代码应该完全按照预期工作(无论是否将字符串转换为数字)我建议您联系您的应用程序供应商,抱怨事情没有按应有的方式运行。

我尝试过这一点,但它没有转换为货币,例如,输出为:
teststring as money:202.2400
它被视为与现有字符串一样,不显示为money。谢谢你到目前为止的帮助。不起作用。您的语法也有点错误,但无论如何,它显然仍然将其视为文本,就好像我改为times:2我得到:
teststring作为money 202.2400202.2400
。我不敢相信从字符串到数字的转换如此困难这是什么样的语言。你必须使用黑客,即使这样也不行,哈哈,对不起。匆忙输入。修复并测试。这很好,但我仍然无法让它工作,问题是因为它试图将字符串显示为货币,但它不工作,因此我需要将字符串转换为数字,这在liquid script中似乎是不可能的。我没有使用Shopify,我使用的是dotmailer,但应该是类似的过程。我当然可以修改数据,在进入dotmailer时将格式化字符串显示为货币(因此不需要使用液体脚本),但这将是一项更大的工作,因为我必须更改大量代码并进行更多测试,在电子邮件中进行格式化(dotmailer中使用liquid脚本)也很有意义,只是为了澄清所需的输出是来自
testnumeric作为货币的输出:£202.24
,但我需要将此应用于字符串,谢谢Hanks Dave B,是的,它是通过dotmailer实现的,因此它必须是它们的实现,因为我已经尝试了一切。我现在已经解决了这个问题,将十进制作为预先格式化为货币的字符串发送——这并不理想,但这是一个解决办法。非常感谢你的帮助。是的,用格式化字符串代替数字让我感到畏缩——但听起来好像dotmailer强迫你这么做了。很高兴你能找到工作!
{% assign teststring =  '202.2400' %}
{% assign testnum = teststring | times: 100 %}
teststring as money {{ testnum | money }}
teststring: 202.2400 
testnumeric: 202.24 
teststring as money: Liquid error: wrong number of arguments (2 for 1) 
testnumeric as money: Liquid error: wrong number of arguments (2 for 1) 
test convert teststring to numeric: 247.24 
test show above as money: Liquid error: wrong number of arguments (2 for 1)
teststring: 202.2400 
testnumeric: 202.24 
teststring as money: $2.02 !! 
testnumeric as money: $2.02 !! 
test convert teststring to numeric: 247.24 
test show above as money: $2.47 !!