2016-09-08 47 views
0

我试图在液体脚本中显示浮点值作为金钱。它似乎只允许你这样做是为了数字,但是我不能一个字符串(转换为数字尽管在SO如下:)Liquid Script:将字符串转换为数字以显示为钱

{% assign teststring = '202.2400' %} 
{% assign testnumeric = 202.2400 %} 
teststring: {{ teststring }} <br/> 
testnumeric: {{ testnumeric }} <br/> 

teststring as money: {{ teststring |money: "GBP" }} <br/> 
testnumeric as money: {{ testnumeric |money: "GBP" }} <br/> 

{% assign testStringAsNumeric = teststring | plus: 45 %} 
test convert teststring to numeric: {{ testStringAsNumeric }} <br/> 
test show above as money: {{ testStringAsNumeric |money: "GBP" }} 

输出以下的建议是:

teststring: 202.2400 
testnumeric: 202.24 
teststring as money: 202.2400 
testnumeric as money: £202.24 
test convert teststring to numeric: 202.24000 
test show above as money: 202.24000 

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

我也尝试了时间过滤器,例如

{% assign testStringAsNumeric = teststring | times: 1 %} 

试验表明上面的钱:{{testStringAsNumeric |钱: “GBP”}}

但这返回一个错误:

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

感谢所有帮助

+0

只是为了澄清所需的输出是'testnumeric作为金钱的输出:£202.24',但我需要这适用于一个字符串,谢谢 – Blingers

回答

0

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

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

当然,这只有在数字已经是正数才能正常工作。

+0

我试过这个,但它并没有转换为货币例如输出是:'测试字符串作为金钱:202.2400' – Blingers

+0

它被视为现有的字符串,它不显示为金钱。感谢您一直以来的帮助。 – Blingers

1

所以我想解决这个问题有几件事情。

首先,当使用钱币过滤器时,数字被解释为cents,例如。

Input: {{ 2000 | money }} 

Output: $20.00 

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

test show above as money: {{ testStringAsNumeric |money: "GBP" }}

,设置哪个货币是要由money滤波器的使用方式是Shopify管理员的内部,而不是液滴作为参数的内部。正因为如此,一旦你设置Shopify的管理里面你的货币,所有你必须写的是:

test show above as money: {{ testStringAsNumeric | money }}

,使用money过滤器,只有两个尾随零将保持不变。

Input: {{ 2040.002020203 | money }} 

Ouput: {{ $20.40 }} 
+0

这很好,但我仍然无法得到它的工作,问题是因为它试图显示一个字符串作为金钱,它不起作用,所以我需要将字符串转换为一个数字,似乎不可能在液体脚本。我没有使用Shopify我正在使用dotmailer,但应该是类似的过程。 – Blingers

+0

我当然可以修改我的数据,将其格式化的字符串显示为金钱,因为它进入点播器(因此不需要使用液体脚本),但这将是一个更大的工作,因为我必须更改大量代码并做更多测试,这也是有意义的做电子邮件格式(在液体脚本中的点播器) – Blingers

0

我们可以很容易地使用JavaScript进行转换,它的实现起来很快且容易。

+0

谢谢我知道,但我不能使用js我必须使用液体脚本,因为它是在dotmailer – Blingers

0

根据this你应该能够做到:

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

没有工作。你的语法也有点不对,但无论如何,它显然仍然把它当作文本来处理,就像我改变时代一样:2我得到:'teststring as money 202.2400202.2400'。我不敢相信,从字符串到数字的转换都很困难。你必须使用黑客,即使这不起作用大声笑。 – Blingers

+0

对不起。输入匆忙。固定和测试。 – bknights

1

这是我得到的关于我的测试库从液体码输出:

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通常如何使用过滤器)给出:

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 !! 

...这似乎按照预期工作。 (是的,我的开发商店的shop.money_format目前是${{ amount }} !!。在某个时候它让我感到很开心。)

您是否试图在应用程序的上下文中使用这些液体语句?如果是这样,那么应用程序对液体代码的解释可能存在问题 - 但正如您所看到的,从纯粹的Shopify角度来看,您的代码应完全按预期工作(无论是否将字符串转换为数字)I会建议联系你的应用程序供应商,抱怨事情不能按照他们应该的方式工作。

+0

感谢戴夫B,是的,它是通过dotmailer,所以它必须是他们的实施,因为我已经尝试了一切。我现在已经解决了这个问题,通过发送小数点作为预先格式化为货币的字符串 - 并不理想,但这是一种解决方法。非常感谢您的帮助。 – Blingers

+0

是的,使用格式化的字符串代替数字让我畏缩 - 但它听起来像点歌迷已经迫使你的手在这。很高兴你能够得到一些工作! –