Jump to content
Sign in to follow this  
vchris

NumberFormat

Recommended Posts

Number format is working good for me in English but in French I need to have spaces between thousands and commas instead of dots. The numberformat function does not have any French options. Is there a way for me to do this?

Share this post


Link to post
Share on other sites

Well, this will work:

<cfoutput><!--- static output --->10000.12<br /><br /><!--- set to variable and output ---><cfset mynum=10000.12>#mynum#<br /><br /><!--- convert to valid number format string and output ---><cfset mynum=numberformat(mynum,"9,999.99")>#mynum#<br /><br /><!--- replace the commas with spaces and output ---><cfset mynum=replace(mynum,","," ","all")>#mynum#<br /><br /><!--- replace decimals with commas and output ---><cfset mynum=replace(mynum,".",",","all")>#mynum#<br /><br /></cfoutput>

but this a PITA to reuse. Enter ColdFusion UDFs . . . .

Share this post


Link to post
Share on other sites

Here is the function, save this as numberformat_french.cfm somewhere accessible to your application:

<cftry>	<cffunction name="numberformat_french" returntype="string" output="false">	<cfargument name="mynum" type="numeric" default="0" required="yes">	<!--- convert to valid number format string --->	<cfset mynum=numberformat(mynum,"9,999.99")>	<!--- replace the commas with spaces --->	<cfset mynum=replace(mynum,","," ","all")>	<!--- replace decimals with commas --->	<cfset mynum=replace(mynum,".",",","all")>	<cfreturn mynum>	</cffunction><cfcatch type="any"><cfset mynum = 0></cfcatch></cftry>

Then, code your CFM page like this:

<cfinclude template="/_udf/numberformat_french.cfm"><cfoutput>#numberformat_french(10000.12)#</cfoutput>

And you should be set.If you haven't used UDFs, this is a perfect example to use them. If you have to do this a lot, then put the include in your application.cfm folder or inside another include that is designed to be called on pages where this functionality is required.Once the include is called all you have to do is wrap your number in the function format and be sure that is somewhere in a <cfoutput>.Thats quick and dirty solution - hope that helps.

Edited by Skemcin
edited cfinclude template path - you'll need to modify that accordingly | edited my comments, removed the [and output] since the UDF is not outputing anything (i just copied the comments from my raw example)

Share this post


Link to post
Share on other sites

Wow thanks a lot! I appreciate your help. I'm integrating your script.

Share this post


Link to post
Share on other sites

Hey no problem.Let me know if you have any question implementing this. It should be pretty straight forward - put the include where ever you want to use the function, then use the function as many times as you like on that page.What is nice about this, if you set you number to a different variable, you can still use it for calculations that your page might require, then when you are ready to display the value in your format, just throw it in this function and you're set.Have fun - you're now using CF UDFs - you are getting sucked in, I can feel it . . . . :)Yes, its that easy, stop trying to convince yourself that its can't be . . . . unlearn the ways of the dark side Luke!!!:)

Edited by Skemcin
I just realized I'm using <cftry> and <cfcatch>. If you haven't used that before its basically inline error handling. If something doesn't compute in the function, then it will simply return a value of 0 for mynum.

Share this post


Link to post
Share on other sites

Implemented successfully! :)...but the dark side is so much more powerful!

Share this post


Link to post
Share on other sites
Implemented successfully! :)...but the dark side is so much more powerful!
lol - that is debatable, regardless, the dark side looses every time!:)Good job, btw, on getting that code worked in.

Share this post


Link to post
Share on other sites

Just realized something, by adding a space between thousands, if the number is in a cell it'll wrap. I changed the space to   Now it is perfect :)

Share this post


Link to post
Share on other sites

nicely done

Share this post


Link to post
Share on other sites

BTW, There is a CF function for this, but I had forgotten about it:LSCurrencyFormat()but I think this partly is based off of where the server is located.I'm just pointing this out in case someone come along afterwards and mentions this function. You have a solution that works and is platform and location independant - so I think you are in good shape - obviously no need to investigate this unless you really want to. :)

Share this post


Link to post
Share on other sites

I have used this function before to get the French month and day names. Worked pretty good. I'm not sure how it would work with numbers though...

Share this post


Link to post
Share on other sites
I have used this function before to get the French month and day names. Worked pretty good. I'm not sure how it would work with numbers though...
Yeah. I've not had to use it, so I'm not sure what considerations there are for implementing it. In any case, you have your solution and it seems to fit well - thats count for a lot more. :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...