Solved

# How can I round to the nearest thousands and show shortened format?

Posted on 2014-08-07
442 Views
I would like to have shortened versions of numbers but I haven't found a function that does this. Maybe I missed something or if somebody else has some snippets they've done.

For examples...

1. 1,200,000 = "1.2M"
2. 1,250,000 = "1.3M"
3. 99,000 = "99K"
4. 99,500 = "100K"

I basically want to render shortened versions for my mobile version and include "M" or "K" for millions and thousand values.
0
Question by:brihol44

LVL 39

Accepted Solution

Something like this...

You have to play with the round and division so that it rounds to the number of decimals you want, this does 1 decimal

``````<cfset theNumber = 12300000>
<cfif theNumber gte 1000000000>
<cfset newNumber = round(theNumber / 100000000) / 10 & "B">
<cfelseif theNumber gte 1000000>
<cfset newNumber = round(theNumber / 100000) / 10 & "M">
<cfelseif theNumber gte 1000>
<cfset newNumber = round(theNumber / 100) / 10 & "K">
<cfelse>
<cfset newNumber = theNumber>
</cfif>
``````
0

LVL 51

Expert Comment

<ot>
I was going to say check cflib.org, but surprisingly I don't think it has a function for that.

Might not be a bad idea for a UDF submission, if anyone's up to it.
</ot>
0

LVL 6

Expert Comment

Agreed agx.  I will put it on my list.  Is there a technical name for this type of date output?  When I googled it earlier I wasn't sure what to google so I just typed in M for million, k for thousands coldfusion and found the ray camden post.

Randy
0

LVL 51

Expert Comment

I am not aware of any standard term for that notation either. It is reminiscent of some financial reports, but even those are not always consistent.
0

LVL 44

Expert Comment

I just posted a VBA solution for an Excel question very similar to this one.
http:Q_28494315.html#a40251958

Step 1. Obtain the base1000 log of the number
Step 2. Divide by that many thousands and use the value from Step 1 to index into an array of suffixes.

I assume the suffixes would be handled with a ListToArray() function like this:
``````<cfSet cSuffixes = "K,M,G,T,P,E,Z,Y">
<cfSet arrSuffixes = ListToArray(cSuffixes, "," , true)>
``````
The math seems like a direct mapping, with the same function names in both the VBA code and the CF code.
0

Author Closing Comment

thx everyone for participating.
0

## Featured Post

### Suggested Solutions

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…