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 »
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
Comments (3) | Post Comment | Ask Ben | Permalink | Print Page
Ask Ben: Updating An Excel Document More Than Once With The POI Utility
OOPhoto - Handling Database Transactions With Ease
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
Posted by Steven N. Severinghaus on Aug 6, 2008 at 12:05 PM
@Steven,
Thanks for the tip; to be honest, my knowledge about internationalization is pretty poor :(
Posted by Ben Nadel on Aug 6, 2008 at 12:23 PM
thanks
where can i found some informations about the LSCurrencyFormat
Posted by Peter on Aug 8, 2008 at 10:28 AM