• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
Jul 10, 2014 Jul 10, 2014

Copy link to clipboard

Copied

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

Views

1.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Jul 11, 2014 Jul 11, 2014

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

Votes

Translate

Translate
Explorer ,
Jul 11, 2014 Jul 11, 2014

Copy link to clipboard

Copied

So, here is the info, the value had changed from 55 to 29 but, still the same issue:

structEmpUBenefits['price_tags']: 29

iDispCostPeriodID: 1

iFlexRound: 3

this is cfdump structEmpUBenefits

struct

billing_rates 29

disp_billing_rates 29.000

disp_coverage $2,000.00

disp_er_share 0.000

disp_price_tags 29.000

option_notes array [empty]

option_prevselect_notes struct

cssclass green

note

empselections.prevselectedoption.note

params array

1 empselections.withcost

2 29.000

price_tags 29

As you can see here, all disp_ elements with decimals because I used the function on it and price_tags no decimal. Another element params array also has 29.000. Below the same data from the broser json formatted.

All 0 decimals are stripped.

"option_details": {

"billing_rates": 29,

"price_tags": 29,

"disp_price_tags_not_approved": 29,

"price_tags_not_approved": 29,

"disp_er_share": 0,

"option_prevselect_notes": {

"cssclass": "green",

"params": [

"empselections.withcost",

29

],

"note": "empselections.prevselectedoption.note"

},

And, I tried to hard code 55 and 55.000 ... all the same. 0's are gone. I used it in function and displayed just straight NumberFormat(55,"_.000"), same result come back from REST call and on actual page without decimals.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 11, 2014 Jul 11, 2014

Copy link to clipboard

Copied

Hmm, still not 100% clear to me how this app works

Btw, remove the #'s in this statement, as they are not needed:

<cfset structOptionDetails = cnf_u_insempnext(qUSelections = APPLICATION.cfc.format.QueryToStruct(#getCurrBenefits#, #getCurrBenefits.CurrentRow#))>

structEmpUBenefits looks all ok. It looks like some evaluation is being performed on the result, and the trailing zeros are removed in the process. Without seeing the whole application code, it will be hard to debug it further, sorry. I am still not sure how you get those JSON results btw. Where does the transformation to JSON(option_details) occur - perhaps that is the problem area?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 11, 2014 Jul 11, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 11, 2014 Jul 11, 2014

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 11, 2014 Jul 11, 2014

Copy link to clipboard

Copied

I am an ?#!^%ot. I used JSON formatter extension. I disabled it and it and it looks ok. Now, the problem is the app itself. Somewhere in javascript it is stripping 0's.

Thanks tribule for your help. I have to shake front developers because they were pointing at me. I have never developed front end and, was totaliy lost.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 11, 2014 Jul 11, 2014

Copy link to clipboard

Copied

Ok, sounds promising. We knew the CF function was ok. Show us the JavaScript and explain what is going wrong. This is why I was asking about the JSON stuff.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 12, 2014 Jul 12, 2014

Copy link to clipboard

Copied

LATEST

genadi_mak wrote:

I used JSON formatter extension. I disabled it and it and it looks ok.

In fact, as my experiments showed earlier,

If you add returnformat=json to the query-string, resulting in, for example,

http://127.0.0.1:8500/workspace/cf11_proj/test.cfc?method=dispCostPeriod&in_decCost=55.000 &in_iRoun...

each of the four URLs will return 55.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation