自定义数字格式功能不能以小数形式返回0' s

时间:2014-07-10 13:40:57

标签: javascript html5 rest coldfusion

我的自定义数字格式化功能不起作用。当站点具有标准客户端服务器体系结构时,它工作正常。但是,当我们改为使用带有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。

我尝试了两种返回类型“string”和“numeric”。我试着直接回来了

<cfreturn NumberFormat(in_decCost,"_.000") />

没有任何作用。

=============================================== =========

更新: 我从常规cfm页面调用了这个函数,它按预期工作并返回55.000。这是函数调用。

  

APPLICATION.cfc.calccostcoverage.dispCostPeriod(55,3)

只是想知道它是否与REST API如何返回值有关。

=============================================== =========

我在Windows Server 2008上使用CF10。

非常感谢任何帮助。

谢谢, 颊

1 个答案:

答案 0 :(得分:0)

改变这个:

<cfset decRoundedNum = NumberFormat(in_decCost, "_." & RepeatString("9",in_iRound))> 

到此:

<cfset decRoundedNum = NumberFormat(in_decCost, "_." & RepeatString("0",in_iRound))> 

根据docs,9是可选数字,而0将始终显示。

相关问题