• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 926
  • Last Modified:

NumberFormat

I've got a decimal field in an SQL database that I can't get to format the way I want on a CF page.

If the value shows 4.600 in the database, I want it to be "4.6" on the screen..

I thought #NumberFormat(fieldname, "9999.999")# would drop the trailing zeroes, but it leaves them on there.  If I wanted the zeroes, I would use #NumberFormat(fieldname, "9999.000")#

I'm thinking it might have something to do with the datatype in the database, which I believe is Decimal(9,3)..I haven't had this problem before.
0
dlewis9
Asked:
dlewis9
  • 2
  • 2
1 Solution
 
nathansCommented:
I am sure there is a better way to do this but here is a answer.  I image you could loop thru until the answer is egual or something like that.

Anyway here is an answer.


Custom Tag name: dropzero.cfm
==============================

<cfif parameterexists(attributes.Num)>
<cfset NumTest1 = #attributes.Num#>
<cfset Test10 = int(NumTest1)>
<cfset Test11 = (int(NumTest1 * 10)/10)>
<cfset Test12 = (int(NumTest1 * 100)/100)>
<cfset Test13 = (int(NumTest1 * 1000)/1000)>
<cfset Test14 = (int(NumTest1 * 10000)/10000)>
<cfset Test15 = (int(NumTest1 * 100000)/100000)>
<cfset Test16 = (int(NumTest1 * 1000000)/1000000)>
<cfset Test17 = (int(NumTest1 * 10000000)/10000000)>
<cfset Test18 = (int(NumTest1 * 100000000)/100000000)>
<cfset Test19 = (int(NumTest1 * 1000000000)/1000000000)>
<cfoutput>
<cfif NumTest1 eq Test10>
#NumberFormat(NumTest1, "9999.")#
<cfelseif NumTest1 eq Test11>
#NumberFormat(NumTest1, "9999._")#
<cfelseif NumTest1 eq Test12>
#NumberFormat(NumTest1, "9999.__")#
<cfelseif NumTest1 eq Test13>
#NumberFormat(NumTest1, "9999.___")#
<cfelseif NumTest1 eq Test14>
#NumberFormat(NumTest1, "9999.____")#
<cfelseif NumTest1 eq Test15>
#NumberFormat(NumTest1, "9999._____")#
<cfelseif NumTest1 eq Test16>
#NumberFormat(NumTest1, "9999.______")#
<cfelseif NumTest1 eq Test17>
#NumberFormat(NumTest1, "9999._______")#
<cfelseif NumTest1 eq Test18>
#NumberFormat(NumTest1, "9999.________")#
<cfelseif NumTest1 eq Test19>
#NumberFormat(NumTest1, "9999._________")#
</cfif>

</cfoutput>
</cfif>



=====================================
Test File for it below
=====================================

<html>
<head>
      <title></title>
</head>

<body>

<cfset NumTest1 =123.244500000>
<cfset NumTest2 =123.200000>
<cfset NumTest3 =123.47433>
<cfset NumTest4 =123.4743000>
<cfoutput>

<cf_dropzero num=#NumTest1#><br>
<cf_dropzero num=#NumTest2#><br>
<cf_dropzero num=#NumTest3#><br>
<cf_dropzero num=#NumTest4#>
</cfoutput>
</body>
</html>
0
 
dlewis9Author Commented:
Ugh -- hate to have to do it that way, but that will work..

0
 
nathansCommented:
Yes, I know but I don't know the better way yet.  If someone tells you a better way let me know.

Nathan Stanford
Mr ColdFusion

PS.  I am writing a book on ColdFusion Tips and Tricks for Real World Applications.

http://www.nsnd.com/cftips
0
 
dlewis9Author Commented:
Geez, i'm losing my memory..I had this same problem today (over a year later) and forgot the answer was solved here..

Anyway, I found another solution today from someone else..

If you add zero to the variable, it seems to wipe out the zeroes...

So you'd do something like:

#Evaluate("fieldname+0")#

Seemed to do the trick for me..
0
 
mts1701Commented:
Or, you could just try wrapping the numberformat() function in a val() function.

#val(numberformat(someVar, "9.9"))#

That'll kill your trailing zeros.



0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now