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

Posted on 2014-08-07
Last Modified: 2014-08-15
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.
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">
       <cfset newNumber = theNumber>

    Open in new window

    LVL 51

    Expert Comment

    I was going to say check, 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.
    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.

    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.
    LVL 44

    Expert Comment

    I just posted a VBA solution for an Excel question very similar to this one.

    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)>

    Open in new window

    The math seems like a direct mapping, with the same function names in both the VBA code and the CF code.

    Author Closing Comment

    thx everyone for participating.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    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 ( . 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…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now