Ask Ben: Moving Decimal Places And Formatting Numbers

Posted August 6, 2008 at 8:48 AM

Tags: ColdFusion, Ask Ben

Hi Ben, I have been trying to use DecimalFormat and NumberFormat to get some integers coming out of a database query to display as decimals numbers. For Example I want 415 to display as 4.15. I also need 6 to display as .60 How might I accomplish this. I really love your writings by the way.

To do this, all you need to do is move over the decimal place and then format the number. Based on those two numbers, it looks like the number of decimal places is not generic. For 415, you want to move it over twice. For 6, you only move it over once. You'll have to figure out that part of the logic, but once you do, the formatting is quite easy:

 Launch code in new window » Download code as text file »

  • <!--- Set both values so that we can mimic database values. --->
  • <cfset intValueA = 415 />
  • <cfset intValueB = 6 />
  •  
  •  
  • <!--- Output first number in decimal format. --->
  • #DecimalFormat( intValueA / 100 )#<br />
  •  
  •  
  • <!---
  • The second number is slightly more tricky since you did not
  • want a leading zero. All of the number formatting options put
  • in a leading zero. Therefore, we have to manually remove that
  • with some list functionality.
  • --->
  • .#ListLast( DecimalFormat( intValueB / 10 ), "." )#

Once of the beauties of a base-10 number system such as ours is that all you need to do to move decimal places is divide by a power of 10. Each power of 10 moves the decimal place over once. Running the above code we get the following output:

4.15
.60

Hope that helps.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Print Page




Reader Comments

Aug 6, 2008 at 12:05 PM // reply »
2 Comments

If those happen to be dollar figures (or any other currency) that you're trying to format, don't forget about LSCurrencyFormat(number)...

http://livedocs.adobe.com/coldfusion/8/htmldocs/functions_l_27.html


Aug 6, 2008 at 12:23 PM // reply »
6,516 Comments

@Steven,

Thanks for the tip; to be honest, my knowledge about internationalization is pretty poor :(


Aug 8, 2008 at 10:28 AM // reply »
1 Comments

thanks
where can i found some informations about the LSCurrencyFormat


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »