# Ask Ben: Moving Decimal Places And Formatting Numbers

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

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.

 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
InVision App - Prototyping Made Beautiful With Prototyping Tools Recent Blog Comments
Mar 9, 2014 at 6:11 PM
For Better Security Use HtmlEditFormat() In Conjunction With JSStringFormat() In ColdFusion
It looks like htmleditformat() will be deprecated in CF 11 https://wikidocs.adobe.com/wiki/display/coldfusionen/New +in+ColdFusion ... read »
Mar 9, 2014 at 10:55 AM
\$.stop() vs. \$.finish() In jQuery Animations
Nice feature! Thanks for sharing. :) Good for when you are making a 100% AJAX controlled site. ... read »
Mar 9, 2014 at 12:26 AM
Experimenting With Multiple Class Inheritance In Javascript
@bigboomshakala, Hi, I am want present so solution for multiple prototypical inheritance, but some restrictions. Please follow the link: http://arto8.site11.com/ArtoJS/multi-inheritance-prototype.ph ... read »
Mar 8, 2014 at 5:21 PM