Jump to content

NumberFormat


vchris

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?

Link to comment
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 . . . .

Link to comment
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.

Link to comment
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!!!:)

Link to comment
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.
Link to comment
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 :)

Link to comment
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. :)

Link to comment
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...

Link to comment
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. :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...