我的自定义数字格式化功能不起作用。当站点具有标准的客户端服务器架构时,它工作得很好。但是,当我们更改为使用带有 javascript 和 HTML5 的 REST API 时,该函数适用于大多数部分,除了我需要它显示小数点 0 的情况。
基本上,该函数接受参数数字和小数位数,并返回四舍五入到传递的小数位数的数字。下面,我将功能剥离到最低限度,与我测试的相同。
<cffunction name="dispCost" access="remote" returntype="string" output="false" hint="Displays costs for a given period to user">
<cfargument name="in_decCost" type="numeric" required="yes" />
<cfargument name="in_iRound" type="numeric" required="yes" />
<cfset decRoundedNum = NumberFormat(in_decCost, "_." & RepeatString("9",in_iRound))>
<cfreturn decRoundedNum />
</cffunction>
当我传递 55.00089 和 3 (dispCost(55.00089,3)) 时,函数返回 55.001。
当我传递 55 或 55.000000 或 55.0000089 和 3 (dispCost(55.0000089,3) 或 dispCost(55.00000,3)) 时,函数返回 55。而且,我需要 55.000。
我尝试了返回类型“字符串”和“数字”。我尝试直接返回
<cfreturn NumberFormat(in_decCost,"_.000") />
没有任何作用。
================================================== =========
更新: 我从常规 cfm 页面调用了这个函数,它按预期工作并返回 55.000。这是函数调用。
APPLICATION.cfc.calccostcoverage.dispCostPeriod(55,3)
只是想知道这是否与 REST API 返回值的方式有关。
================================================== =========
我在 Windows Server 2008 上使用 CF10。
非常感谢任何帮助。
谢谢, 吉纳
最佳答案
更改此:
<cfset decRoundedNum = NumberFormat(in_decCost, "_." & RepeatString("9",in_iRound))>
对此:
<cfset decRoundedNum = NumberFormat(in_decCost, "_." & RepeatString("0",in_iRound))>
根据 docs , 9 是可选数字,而 0 将始终出现。
关于javascript - 自定义数字格式函数无法将 0 作为小数返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24678102/