1

Does handlebar support currency formatting? Something like below for instance?

IDR{{amount}}
SGD{{amount}}
Naman
  • 27,789
  • 26
  • 218
  • 353
Minisha
  • 2,117
  • 2
  • 25
  • 56

2 Answers2

1

yes it is possible to use.

 {{numberFormat number ["format"] [locale=default]}}

this is the general use. Format parameters are used as integers, percentages, currency, or the default formatter.

for currency

{{numberFormat 1.638 "currency"}} 

Output: $1.63

for different currency

{{numberFormat 1.638 "currency" "de_DE"}}
Output: 1,63 €

if you want to use this format for percent

{{numberFormat .46 "percent"}}
Output: 46%
0

Handlebar.java supports currency formatting for numerical values only.

So, if your data looks like... { "amount" : 32 }
...you're fine using selahaddin aksu's answer.

However, if your 'number' is actually a string value... { "amount" : "32" }
...you will need to use an additional helper to convert the string to a number datatype first, otherwise your output will return the string value of "currency" instead of a formatted number.

You can use this custom helper...

Handlebars.registerHelper("toNum", function (n) {
    return parseFloat(n) || +n;
});

...like this...

1: "amount": 27.5,
2: "amount": "72.3",
3: "amount": -32,
4: "amount": "-23"

{{numberFormat (toNum amount1) "currency"}}

OUTPUT -
1: $27.50
2: $72.30
3: -$32.00
4: -$23.00
MistyDawn
  • 856
  • 8
  • 9