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

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
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.

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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.
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.
brihol44Author Commented:
thx everyone for participating.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.