I have some arbitrary number, which in my use-case is between 1,000 and 1,000,000. I need to format this value into a display value, in which it's being displayed as money. I.e 1000
-> "$1,000"
, 100000
->"$100,000"
, etc.
The problem I'm having is that my expression size is too large, and the AMP expressions are too limiting, so it's not easy to format numbers the way I wanted to. I can't use ".replace()", or any regex, and even using basic conditionals seems overly difficult (doesn't seem like I can even use a standard ternary operator: a?b:c;).
Here's what I have
//tells me how many digits i have
amp-bind-macro#getLog10(arguments="num" expression="num.length - 1")
//determines if the number should have a comma after it
amp-bind-macro#numShouldHaveComma(arguments="log10" expression="log10%3==0 && log10!=0")
//function to be invoked by Array.map()
amp-bind-macro#formatNumber_map(arguments="char, ind, num" expression="(numShouldHaveComma(getLog10(round(num).toString().substr(ind+1))) && char+',') || char")
//main formatter function (1000 -> 1,000)
amp-bind-macro#formatNumber(arguments="num" expression="round(num).toString().split('').map((char,ind)=>formatNumber_map(char,ind, num)).join('')" )
//adds $ and calls the formatter
amp-bind-macro#formatMoney(arguments="val" expression="'$'+formatNumber(val)")
I have a display element set to call formatMoney when a slider's value changes, e.g.
<input type='range' on="input-throttled:AMP.setState({state:{mySlider:{value:event.value}}})" />
and
<div id='display-money' [text]="formatMoney(state.mySlider.value)">$1,000</div>
This particular way of doing it leaves me with a stack size of 53, which is more than the allowed maximum of 50.
The reason I do round(num).toString()
is that I seemed to be getting inconsistent types - sometimes it's a Number, and other times a String. This way, the type is always parsed correctly, and no errors are thrown.
Is there a simpler way to format a number to look like money (whole dollars, comma-delimited by thousands)? If not, what can I do to my existing code to make it work?
Thanks!