[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 476
  • Last Modified:

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.
0
brihol44
Asked:
brihol44
1 Solution
 
gdemariaCommented:
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>

Open in new window

0
 
_agx_Commented:
<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
 
rjohnsonjrCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
_agx_Commented:
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
 
aikimarkCommented:
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)>

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.
0
 
brihol44Author Commented:
thx everyone for participating.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now