簡體   English   中英

液體腳本:將字符串轉換為數字以便顯示為貨幣

[英]Liquid Script: Convert string to number in order to display as money

我試圖在液體腳本中將浮點值顯示為貨幣。 它似乎只允許您對數字執行此操作,但是我無法將字符串轉換為數字(盡管遵循以下有關SO的建議:)

{% 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

我想要的是將字符串值(測試字符串)顯示為金錢。 因此,我需要轉換為數字,然后顯示為貨幣。

我也嘗試過時間過濾器

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

上面的測試顯示為金錢:{{testStringAsNumeric |金錢:“ GBP”}}

但這返回一個錯誤:

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

謝謝你的幫助

我想解決這個問題。

首先 ,當使用貨幣過濾器時,數字解釋為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 }}

這是我在測試存儲中從您的臨時代碼中得到的輸出:

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: "GBP"過濾器變得簡單| money | 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角度來看,您的代碼應完全按預期工作(將字符串轉換為數字或不將字符串轉換為數字)建議您與您的應用程序供應商聯系,以抱怨事情沒有按照應有的方式進行。

如果只有正數,則可以使用abs過濾器將String轉換為數字。 這應該是這樣的工作:

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

當然,只有在數字為正數的情況下,此方法才能正確運行。

根據這個你應該能夠做到:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM