Skip to main content
Inspiring
July 10, 2014
Answered

Custom number format function does not work to return 0's as decimals

  • July 10, 2014
  • 2 replies
  • 2527 views

Hi

My custom number formatting function does not work. It was working fine when the site had a standard client server architecture. However, when we changed to using REST API with javascript and HTML5 the function works for most parts except cases when I need it to display 0's for decimals.

Basically, the function takes arguments a number and number of decimals and, returns the number rounded to the number of decimals passed. Below, I stripped the function to bare minimum, same as I was testing.

<cffunction name="dispCostPeriod" 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>

When I pass 55.00089 and 3, the function returns 55.001.

When I pass 55 or 55.000000 or 55.0000089 and 3, the function returns 55. And, I need 55.000.

I tried both to returntype "string" and "numeric", various masks like _.999, _.000, _.___.

I tried straight return NumberFormat(in_decCost,"_.000").

Nothing works.

I am using CF10 on Windows Server 2008.

Any help greatly appreciated.

Thanks,

Gena

    This topic has been closed for replies.
    Correct answer tribule

    How the works? I am not sure how to describe.

    I tried to display a NumberFormat(55,"0.00")

    This is how it looks in browser as json formatted raw data. Not bumped around in structures and arrays inside the app. This is a top structure element that is set on 5th line of the REST component.

    "testnumber": 55,

    "success": true

    And here is the kicker, I tried the same in Chrome REST Console this is how it looks.

    1.    "testnumber": 55.000,
    2.    "success": true


    Can someone explain this? Why in Chrome REST Console the data looks as expected with 0's but, in browser window as a json formatted raw data 0's are truncated.


    Can you explain how the data gets into the browser as a JSON formatted string?

    2 replies

    Inspiring
    July 10, 2014

    UPDATE: I called this very function from regular cfm page like that 

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

    And, it works as expected and returns 55.000.

    Just wondering if it is something to do with how REST API returning values.

    BKBK
    Community Expert
    Community Expert
    July 10, 2014

    You say you need a numeric value, but your function has string returntype. You should change the return type to numeric.

    Inspiring
    July 10, 2014

    I know I did, I tried different variations of the returntype, I mentioned this in my original post. The returned value is treated as numeric by javascript in the front end of the app and if it cant convert it to a number, the app crashes. So, even I left the returntype as a string, it is implicitly converted to a number later.

    BKBK
    Community Expert
    Community Expert
    July 10, 2014

    What about the mask "00.000" or "99.999"? That is, using your syntax, "00." & RepeatString("0",in_iRound) or "99." & RepeatString("9",in_iRound).

    Inspiring
    July 10, 2014

    "00.000" crashes the app. I tried "__.000". It returns " 55.000" as a string and further crashes the app. I need numeric value that will be evaluated further as a number.