Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amp html 如何在AMP html中将数字格式化为货币?_Amp Html_Accelerated Mobile Page_Google Amp_Amp Bind - Fatal编程技术网

Amp html 如何在AMP html中将数字格式化为货币?

Amp html 如何在AMP html中将数字格式化为货币?,amp-html,accelerated-mobile-page,google-amp,amp-bind,Amp Html,Accelerated Mobile Page,Google Amp,Amp Bind,我有一些任意的数字,在我的用例中是1000到1000000之间。我需要将这个值格式化为一个显示值,在这个显示值中它被显示为货币。即1000->“$1000”,100000->“$100000”,等等 我遇到的问题是,我的表达式太大,而AMP表达式太有限,所以按我想要的方式格式化数字并不容易。我不能使用“.replace()”或任何正则表达式,甚至使用基本条件似乎也太难了(我甚至不能使用标准的三元运算符:a?b:c;) 这是我的 //tells me how many digits i hav

我有一些任意的数字,在我的用例中是1000到1000000之间。我需要将这个值格式化为一个显示值,在这个显示值中它被显示为货币。即
1000
->
“$1000”
100000
->
“$100000”
,等等

我遇到的问题是,我的表达式太大,而AMP表达式太有限,所以按我想要的方式格式化数字并不容易。我不能使用“.replace()”或任何正则表达式,甚至使用基本条件似乎也太难了(我甚至不能使用标准的三元运算符:a?b:c;)

这是我的

  //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)")
我设置了一个显示元素,当滑块的值改变时调用formatMoney,例如

<input type='range' on="input-throttled:AMP.setState({state:{mySlider:{value:event.value}}})" /> 

1000美元
这种特殊的方法使我的堆栈大小为53,这超过了允许的最大值50

我之所以进行
round(num).toString()
是因为我似乎得到了不一致的类型——有时是数字,有时是字符串。这样,类型总是被正确解析,并且不会抛出错误

是否有一种更简单的方法将数字格式化为货币(整美元,逗号以千分隔)?如果没有,我可以对现有代码做些什么来使其工作


谢谢

我提出的解决方案如下

//determines how many "0's" there are.
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")

//helper function for formatNumber
amp-bind-macro#formatNumber_map(arguments="char, ind, numStr" expression="(numShouldHaveComma(getLog10(numStr.substr(ind)))) ? char+',' : char")

//main number formatter
amp-bind-macro#formatNumber(arguments="num, numStr" expression="numStr.split('').map((char,ind)=>formatNumber_map(char,ind, numStr)).join('')" )

//adds "$" and calls formatNumber
amp-bind-macro#formatMoney(arguments="val" expression="'$'+formatNumber(round(val), round(val).toString())")

本质上,我简化了表达式,将数字作为数字和字符串显式传递给下一个函数,这样我就不需要在每个宏中调用round(num).toString(),只需调用第一个宏。

对于整数大小写
formatNumber
,我建议使用更紧凑的实现,在一个
amp bind宏中

<amp-bind-macro id="formatNumber" arguments="value" expression="(value<0?'-':'')+(abs(value)||0).toFixed().split('').map((v,i,a)=>(i&&(a.length-i)%3==0?',':'')+v).join('')"></amp-bind-macro>

Sign$我们可以使用CSS::after,它工作正常
<amp-bind-macro id="formatNumber" arguments="value" expression="(value<0?'-':'')+(abs(value)||0).toFixed().split('').map((v,i,a)=>(i&&(a.length-i)%3==0?',':'')+v).join('')"></amp-bind-macro>