2016-09-08 44 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

是的,使用格式化的字符串代替數字讓我畏縮 - 但它聽起來像點歌迷已經迫使你的手在這。很高興你能夠得到一些工作! –