House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

Rounding to 2 decimal places

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Chris Ditty
04/03/2007 11:39 AM

Someone please tell me that this is so simple that I am just missing it.  I am trying to round this number - 207.44047619 to 207.44. The only way that I can think to do this would be to roll a custom function.  Is there not a 1-2 liner method to do this that I am simply overlooking?

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Adrian Lynch
04/03/2007 11:45 AM

Check cflib.org, I seem to remember there being something on that for this problem. Adrian Someone please tell me that this is so simple that I am just missing it.  I am trying to round this number - 207.44047619 to 207.44. The only way that I can think to do this would be to roll a custom function.  Is there not a 1-2 liner method to do this that I am simply overlooking?

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Chris Ditty
04/03/2007 11:54 AM

Thanks.  That fixed it. I was looking under math functions and didn't think about looking under format.  DUH.  :) thanks again ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Barney Boisvert
04/03/2007 12:00 PM

#round(num * 100) / 100# will do just the math part.  I.e. it won't add commas for thousand separators or an extra trailing zero.  If you need to round the number and get a number (rather than a display string), this method is preferable.  That code is obviously suitable for any number of decimal places, you need to raise 10 to the power of the number of places you want and use it where I've used 100 (which is 10 ^ 2). cheers, barneyb ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Peter Boughton
04/03/2007 12:51 PM

You can avoid the round function by doing integer division: #num*100\100# ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Barney Boisvert
04/03/2007 01:11 PM

That will truncate, leaving you with zero decimal places, not two. This expression:   #((num * 100) \ 1) / 100# will do as you want, but it has the effect of truncating at two decimal places, rather than rouding to two decimal places.  E.g., 123.4567 will become 123.45, rather than the desired 123.46.  Not to mention the fact that the round() call is much more readable. cheers, barneyb ----- Excess quoted text cut - see Original Post for more ----- -- Barney Boisvert bboisvert@gmail.com http://www.barneyb.com/ Got Gmail? I have 100 invites.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Peter Boughton
04/03/2007 02:19 PM

Yeah, dunno what I was thinking there. Not sure if I actually woke up this morning; I've been doing similar things through-out today. :( ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Peter Boughton
04/03/2007 01:18 PM

Wait - ignore me, I'm going crazy. That doesn't work. :$ >You can avoid the round function by doing integer division: > >#num*100\100#

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Peterson, Chris
04/03/2007 01:58 PM

You could do numberFormat(varName, "0.00") also Chris Thanks.  That fixed it. I was looking under math functions and didn't think about looking under format.  DUH.  :) thanks again ----- Excess quoted text cut - see Original Post for more -----


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

November 26, 2014

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
             1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30             

Designer, Developer and mobile workflow conference