javascript - 自定义数字格式函数无法将 0 作为小数返回

标签 javascript html 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。

我尝试了返回类型“字符串”和“数字”。我尝试直接返回

<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/

相关文章:

javascript - 如何使用 Javascript 从文本框中获取文本的值

angularjs - Angular js获取由div分隔的表单标签中元素的$valid值

spring - 无法导入Spring RepositoryRestResource

javascript - 在Sinon 中,spyOn.and.callFake 相当于什么?

javascript - 有没有一种方法可以在编写函数后在函数中拥有多个输入?

javascript - 如何使用 dojo 提交使用 spring 表单标签创建的表单

javascript - 可调整大小的侧边栏 : How to set the maximum width of the main area

html - CDATA 不适用于 <pre> 标记内的 C++ 代码

node.js - GraphQL 的 SOAP 转换

java - 如何在从 swagger 生成 java 代码时将 "double"字段视为 "BigDecimal"?