# Ask Ben: Moving Decimal Places And Formatting Numbers

Posted August 6, 2008 at 8:48 AM by Ben Nadel

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:

• <!--- 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.

### Looking For a New Job?

25% of job board revenue is donated to Kiva. Loans that change lives - Find out more »

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

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

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

@Steven,

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

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

thanks
where can i found some informations about the LSCurrencyFormat

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

 Author Name: Author Email: Author Website: Comment: Supported HTML tags for formatting: bold   italic   code Remember my information Subscribe to comments Send me a copy of this comment
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Dec 11, 2013 at 10:02 PM
Setting ETag Headers Regardless Of File Content In ColdFusion
Hey Ben, got a quick question for you. I have added a cache manifest file for HTML5 coding and made use of the cfcache tag as such; placed above the body tag. When placing the cfcache with the funct ... read »
Dec 11, 2013 at 4:27 PM
Creating A Custom Show / Hide Directive In AngularJS
This does not work for me. Tried everything as is. ... read »
Dec 11, 2013 at 12:23 PM
Preloading Data Before Executing ngInclude In AngularJS
You could simply use a variable for the src attribute that will be published after the data is loaded and a ng-hide to not show the container while loading the html, which will be switched by the onl ... read »
Dec 11, 2013 at 3:47 AM
ColdFusion 10 - An Augmented Virtual File System (VFS)
I recently found the VFS in both ColdFusion 9 and 10 has some weird performance curves when writing a lot of in-memory files. ( I tried this because I wanted to create a high performance log entry w ... read »
Dec 10, 2013 at 1:00 PM
Quiet: The Power Of Introverts In A World That Can't Stop Talking By Susan Cain
Wonderful insights of your own here. Would love to read this book. Each day of our life we are surrounded with both introverts and extroverts.,, we consciously or subconsciously learn of people every ... read »
Dec 10, 2013 at 12:36 PM
The Processing Instruction Target Matching "[xX][mM][lL]" is Not Allowed
Thanks a lot Ben! Can't count the times i've googled your blog! ... read »
Dec 10, 2013 at 9:31 AM
Changes In Transclude() Function Availability In AngularJS 1.2
With Angular it seems more powerful but less mature as a platform. One of the struggles CFML has is there are not as many solutions as richly supported by developers as WordPress. We have some CMS so ... read »
Dec 9, 2013 at 4:53 PM
Using Track-By With ngRepeat In AngularJS 1.2
Make sure that you put the "track by" syntax after any filters. https://github.com/tastejs/todomvc/pull/726#issuecomment-28817720 ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools