Solved

NumberFormat

Posted on 2000-03-30
5
914 Views
Last Modified: 2013-12-24
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
Comment
Question by:dlewis9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
nathans earned 50 total points
ID: 2671008
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
 
LVL 2

Author Comment

by:dlewis9
ID: 2673681
Ugh -- hate to have to do it that way, but that will work..

0
 
LVL 5

Expert Comment

by:nathans
ID: 2673939
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
 
LVL 2

Author Comment

by:dlewis9
ID: 6084107
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
 

Expert Comment

by:mts1701
ID: 12096092
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ColdFusion Rereplace 3 96
Webserver access problem 5 78
How code a 301 redirect for folder files -> 1 file 2 78
htaccess block all countries but the US 4 96
In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question