How to convert numbers into words using liquid coding?

I want to show Numbers to Words.

Total Amount to Total Amount in words in liquid coding.

any liquid expert, Please help me.

Give some code, where it will solve.

@shapnobuz Do you want ti display only at product page level? You will also need to use javascript to achieve this if the prices are different at variant level and you need the prices in words on cart page too.

Check this for code https://www.thoughtco.com/how-to-convert-numbers-to-words-with-javascript-4072535

If someone wants it in Liquid for Order Printer template, this simple code will help. The words is for amount upto 999.99. The code can be extended, if there are more digits.

Please add the definition top of the template:

{% assign hwords = ‘Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Zero’ | split: ‘,’ %}
{% assign twords = ‘Zero,Ten,Twenty,Thirty,Forty,Fifty,Sixty,Seventy,Eighty,Ninety’ | split: ‘,’ %}
{% assign digit = ‘Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Zero’ | split: ‘,’ %}
{% assign tdigit = ‘Zero,Eleven,Twelve,Thirteen,Fourteen,Fifteen,Sixteen,Seventeen,Eighteen,Nineteen’ | split: ‘,’ %}
{% assign test = “300.25” %} {% comment %} remove lines for test {% endcomment %}

Place below code where your amount in words should appear. Change the variable total1 to whatever your variable name in your template.

{% comment %} for 6 digits {% endcomment %}
{% assign total1 = test %} {% assign s = total1.size %} {% if s == 6 %}{% assign x1 = total1 | slice: 0 %}

{% assign x1 = x1 | plus: 0 %}{{ hwords[x1] }} Hundred {% assign x1 = total1 | slice: 1, 2 %} {% assign x1 = x1 | plus: 0 %} {% if x1 > 10 and x1 < 20 %} {% assign x1 = x1 | minus: 10 %} {{ tdigit[x1] }}
{% else %} {% assign x1 = total1 | slice: 1 %} {% assign x1 = x1 | plus: 0 %}{% if x1 != 0 %} {{ twords[x1] }} {% endif %}
{% assign x1 = total1 | slice: 2 %} {% assign x1 = x1 | plus: 0 %} {% if x1 != 0 %} {{ digit[x1] }}
{% endif %} {% endif %}

{% assign x1 = total1 | slice: 4, 2 %} {{ x1 }} {% assign x1 = x1 | plus: 0 %} {% if x1 > 10 and x1 < 20 %} {% assign x1 = x1 | minus: 10 %} and Cents {{ tdigit[x1] }}
{% else %} {% assign x1 = total1 | slice: 4 %} {% assign x1 = x1 | plus: 0 %} and Cents {% if x1 != 0 %} {{ twords[x1] }} {% endif %} {% assign x1 = total1 | slice: 5 %} {% assign x1 = x1 | plus: 0 %} {% if x1 != 0 %}{{ digit[x1] }} {% endif %} {% endif %}
Only {% endif %}

{% comment %} for 5 digits {% endcomment %}
{% assign total1 = test %} {% assign s = total1.size %} {% if s == 5 %}{% assign x1 = total1 | slice: 0 %}

{% assign x1 = total1 | slice: 0, 2 %} {% assign x1 = x1 | plus: 0 %} {% if x1 > 10 and x1 < 20 %} {% assign x1 = x1 | minus: 10 %} {{ tdigit[x1] }}
{% else %} {% assign x1 = total1 | slice: 0 %} {% assign x1 = x1 | plus: 0 %}{% if x1 != 0 %} {{ twords[x1] }} {% endif %}
{% assign x1 = total1 | slice: 1 %} {% assign x1 = x1 | plus: 0 %} {% if x1 != 0 %} {{ digit[x1] }}
{% endif %} {% endif %}

{% assign x1 = total1 | slice: 3, 2 %} {{ x1 }} {% assign x1 = x1 | plus: 0 %} {% if x1 > 10 and x1 < 20 %} {% assign x1 = x1 | minus: 10 %} and Cents {{ tdigit[x1] }}
{% else %} {% assign x1 = total1 | slice: 3 %} {% assign x1 = x1 | plus: 0 %} and Cents {% if x1 != 0 %} {{ twords[x1] }} {% endif %} {% assign x1 = total1 | slice: 4 %} {% assign x1 = x1 | plus: 0 %} {% if x1 != 0 %}{{ digit[x1] }} {% endif %} {% endif %}
Only {% endif %}

{% comment %} for 4 digits {% endcomment %}
{% assign total1 = test %} {% assign s = total1.size %} {% if s == 4 %}

{% assign x1 = total1 | slice: 0 %} {% assign x1 = x1 | plus: 0 %}{% if x1 != 0 %} {{ digit[x1] }} {% endif %}

{% assign x1 = total1 | slice: 2, 2 %} {{ x1 }} {% assign x1 = x1 | plus: 0 %} {% if x1 > 10 and x1 < 20 %} {% assign x1 = x1 | minus: 10 %} and Cents {{ tdigits[x1] }}
{% else %} {% assign x1 = total1 | slice: 2 %} {% assign x1 = x1 | plus: 0 %} and Cents {% if x1 != 0 %} {{ twords[x1] }} {% endif %} {% assign x1 = total1 | slice: 3 %} {% assign x1 = x1 | plus: 0 %} {% if x1 != 0 %}{{ digit[x1] }} {% endif %} {% endif %}
Only {% endif %}

Hope this code helps someone who doesn’t have large amounts.

1 Like

The below code will work for upto 99999.99. The following is actually to convert Indian currency amount into words.

{% assign rupees = order.total_price | divided_by:100 %}
{% assign paisa = order.total_price | modulo:100 %}

{% assign ones = ‘,One,Two,Three,Four,Five,Six,Seven,Eight,Nine’| split:‘,’ %}
{% assign teens = ‘Ten,Eleven,Twelve,Thirteen,Fourteen,Fifteen,Sixteen,Seventeen,Eighteen,Nineteen,’| split:‘,’ %}
{% assign tens = ‘,Twenty,Thirty,Forty,Fifty,Sixty,Seventy,Eighty,Ninety’| split:‘,’ %}

{% if rupees < 100000 %}
{% if rupees > 0 and 100000 %}
{% assign thTen = rupees | divided_by:10000 %}
{% assign thOne = rupees | modulo:10000 | divided_by:1000 %}
{% assign thTeen = 10 %}
{% if thTen == 1 %}
{% assign thTeen = rupees | divided_by:1000 | modulo:10 %}
{% assign thOne = 0 %}
{% endif %}

{% assign hn = rupees | modulo:1000 | divided_by:100 %}

{% assign ten = rupees | modulo:100 | divided_by:10 %}
{% assign teen = 10 %}
{% assign one = rupees | modulo:10 %}
{% if ten == 1 %}
{% assign teen = rupees | modulo:100 | modulo:10 %}
{% assign one = 0 %}
{% endif %}
{% if thTen !=0 or thOne !=0 or thTeen != 10 %}
{{ tens[thTen] }}
{{ teens[thTeen] }}
{{ ones[thOne] }} Thousand
{% endif %}
{% if hn != 0 %}
{{ ones[hn] }} Hundred and
{% endif %}
{{ tens[ten] }}
{{ teens[teen] }}
{{ ones[one] }} Rupees
{% endif %}

{% if paisa > 0 %}
{% assign pten = paisa | modulo:100 | divided_by:10 %}
{% assign pteen = 10 %}
{% assign pone = paisa | modulo:10 %}
{% if pten == 1 %}
{% assign pteen = paisa | modulo:100 | modulo:10 %}
{% assign pone = 0 %}
{% endif %}
{{ tens[pten] }}
{{ teens[pteen] }}
{{ ones[pone] }} Paisa
{% endif %}
{% endif %}

1 Like