1

I'm trying to display a floating point value as money in liquid script. It appears to only allow you to do this to numbers, however I can't convert a string to a number (despite following advice on SO as below:)

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

The output is:

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

What I want is to show the string value (teststring) as money. So I need to convert to a number and then display as money.

I've also tried the times filter e.g.

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

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

But this returns an error:

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

Thanks for any help

Blingers
  • 789
  • 1
  • 9
  • 22
  • Just to clarify the desired output is the output from `testnumeric as money: £202.24` but I need this applying to a string, thanks – Blingers Sep 09 '16 at 08:16

4 Answers4

2

So there are a couple things with this question I'd like to address.

Firstly, when using the money filter, the number is interpreted as cents, eg.

Input: {{ 2000 | money }}

Output: $20.00

Because of this, your line below, once correctly formatted, will display $2.47.

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

Second, the way to set which currency is going to be used by the money filter is inside Shopify admin, rather than inside the liquid drop as a parameter. Because of this, once you set your currency inside Shopify's admin, all you'll have to write is:

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

Third, using the money filter, only two trailing zeroes will be kept.

Input: {{ 2040.002020203 | money }}

Ouput: {{ $20.40 }}
Keyfer Mathewson
  • 1,035
  • 2
  • 16
  • 27
  • This is fine but I still can't get it to work, the issue is because it is trying to display a string as money and it doesn't work, so I need to convert the string to a number which seems impossible in liquid script. I'm not using Shopify I'm using dotmailer but should be similar process. – Blingers Sep 09 '16 at 08:13
  • I could of course amend my data to display the formatted string as money when it goes into dotmailer (and thus not needing to use liquid script) but this will be a much bigger job because I have to change a lot of code and do more testing, it also make sense to do the formatting in the email (in liquid script in dotmailer) – Blingers Sep 09 '16 at 08:15
1

This is what I'm getting as output from your liquid code on my test store:

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)

Changing the | money: "GBP" filter to be simply | money (how Shopify normally uses the filter) gives:

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

... which seems to be working as intended. (And yes, my dev store's shop.money_format is currently ${{ amount }} !!. It amused me at some point.)

Are you trying to use these liquid statements in the context of an app? If so, there may be a problem with that app's interpretation of the liquid code - but as you can see, from a purely Shopify standpoint, your code should be working exactly as expected (both with and without converting the string to a number) I would recommend contacting your app supplier to complain that things aren't working the way they should.

Dave B
  • 3,038
  • 1
  • 13
  • 18
  • Thanks Dave B, yes it is via dotmailer and so it has to be their implementation as I've tried everything. I've resolved this now by sending the decimal as a string pre-formatted as currency - not ideal but it was a workaround. Many thanks for your help. – Blingers Sep 15 '16 at 12:19
  • Yeah, using formatted strings in place of numbers makes me cringe - but it sounds like dotmailer has forced your hand in this. Glad you were able to get something working! – Dave B Sep 15 '16 at 21:59
0

If you have only positive numbers, you can use the abs filter to convert the String to a number. This should the work like this:

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

Of course, this only works correctly if the number is already positive.

Holger Just
  • 52,918
  • 14
  • 115
  • 123
0

According to this you should be able to do:

{% assign teststring =  '202.2400' %}
{% assign testnum = teststring | times: 100 %}
teststring as money {{ testnum | money }}
Community
  • 1
  • 1
bknights
  • 14,408
  • 2
  • 18
  • 31
  • Doesn't work. You have the syntax slightly wrong too but anyway it is obviously still treating it as text as if I change to times: 2 I get: `teststring as money 202.2400202.2400`. I can't believe it's so difficult to convert from string to number what sort of language is this. You have to use a hack and even that doesn't work lol. – Blingers Sep 09 '16 at 08:09
  • Sorry. Typed in hasted. Fixed and tested. – bknights Sep 09 '16 at 14:39