. . . . . . do some stuff on inetegers only. . . . . . . . . . . . convert result back . . . . . . P.S. Dollars behave similary [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => [ref] => [htmlstate] => on_nl2br [postusername] => Mr Black [ip] => webforumsuser@m [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 6 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> Serious round() problem - Macromedia ColdFusion

Serious round() problem - Macromedia ColdFusion

I just discovered an error (with serious consequences) in my code. When I write #Round((0.5053*100) * 250)# I get 12632. But when I write #Round(50.53 * 250)# I get 12633. Which is of course rather surprising at first sight because mathematically both should have the same result. I suppose this has to do with the way decimal numbers are converted to floating point binary numbers. But it scares me because in my program these numbers are amounts of money and they should be correct. There are official rules in my company about how to calculate and round numbers (with euro ...

  1. #1

    Default Serious round() problem

    I just discovered an error (with serious consequences) in my code. When I write

    #Round((0.5053*100) * 250)#

    I get 12632. But when I write

    #Round(50.53 * 250)#

    I get 12633. Which is of course rather surprising at first sight because
    mathematically both should have the same result. I suppose this has to do with
    the way decimal numbers are converted to floating point binary numbers. But it
    scares me because in my program these numbers are amounts of money and they
    should be correct. There are official rules in my company about how to
    calculate and round numbers (with euro and eurocents). The second result
    (12633) is correct to me, but the problem is that the numbers 0.5053 and 250
    (could be anything else) are coming directly from the database and that I must
    multiply them first with 100 in my code (so I always get the wrong result).
    It's worse: this kind of rounding happens many times on almost all of my pages.
    How can I correct this situation or deal with it? Of course this error isn't
    seen with every random number.

    ldetant2 Guest

  2. #2

    Default Re: Serious round() problem

    hi

    yeap...i think there is a problem with CF when rounding the value.
    try this way if possible:

    #round(100*250*.5053)#
    vkunirs Guest

  3. #3

    Default Re: Serious round() problem

    Thanks for the quick reply. This does return the correct result in this case,
    with these numbers. But there can stil be cases where this doesn't work. Take
    for example:

    #Round(100 * 12.4737 * 50)#

    This has the result 62368. But it should be 62369. So it's still not correct.

    ldetant2 Guest

  4. #4

    Default Re: Serious round() problem

    hi

    I think by seeing all these things it better to do it manually like this.

    <cFSET X="12345.3">
    <cfif #val(mid(X,find(".",X)+1,len(x)))# gte 5 >
    #round(x)#
    <Cfelse>
    #Int(x)#
    </cfif>

    try this with your requirements...if you still has the problem let post it
    here...

    vkunirs Guest

  5. #5

    Default Re: Serious round() problem

    Thanks, this indeed looks as a simple but correct alternative. Haven't tried it
    out though because I tried a similar approach: instead of writing:

    #Round( X * Y * Z)#

    I now write:

    #Round(Val(ToString(X * Y * Z)))#

    Up until now this always gives the correct result. But thanks for your advice.
    I'll post your answer in my archive also, so if my approach will show leaks in
    a few days, I'll try yours. Thanks to you and to the forum. This was a great
    help and i'm more assured now. Because I never write the Round() function
    anywhere in my code pages. I created a ColdFusion function which does the
    rounding for me. So simply adapting that function (which I placed in
    Application.cfm) has solved all the rounding problems on all my pages (which
    is a lot!) at once.

    Lieven



    ldetant2 Guest

  6. #6

    Default Re: Serious round() problem

    Hmm... You accepted and is going to advertise absolutely incorrect solution...

    The physics of this problem, as you partially mentioned yourself, is in
    differences between an ideal math and the math with approximate numbers. In
    your case, you are getting different results, because (0.5053*100) and 53.50
    are DIFFERENT numbers (from computer's point of view, of course). Therefore,
    you are rounding different numbers and getting [correct] different results.

    Converting numbers to strings has very little to do with the real solution,
    even it (amazingly) helps sometimes. And it helps, because when you do the
    conversion, CF implicitly applies an internal default rounding algorithm
    (different in different versions - and now Java version dependent). If you
    want to be in control (you are counting money, as far as I understand), you
    should not do this.

    The correct approach would be applying your own rounding rules explicitly.
    So, if 0.5053 is the number of euros and you want to get the number of cents
    with the 2-decimal precision, you should:

    <cfset cents=NumberFormat(0.5053 * 100, "____________.__")>
    <cfset res=Round(cents*250)>

    Also, it is a good idea to setup fields in your database as DECIMAL, not as
    FLOAT or REAL.. Also, before inserting a value in the database, make sure you
    have the right precision, otherwise CF again will "fireindy" round it for you.
    And not necceseraly the way you expect.

    Another common approach is to use integer math everywhere, where possible. If
    your calculations go only up to whole cents, keep all numbers as integer cents,
    not as floating point euros. If you need decimal places, use an appropriate
    scale.

    In your previous example:

    <!--- Convert to integer --->
    <cfset cents=Round(0.5053*1000)>
    . . . . . . do some stuff on inetegers only. . . . . .
    . . . . . . convert result back . . . . . .
    <cfset result=NumberFormat(0.0001*result, . . . . . .>


    P.S. Dollars behave similary





    Mr Black Guest

  7. #7

    Default Re: Serious round() problem

    That is just a bug.

    12632.5 shouldn't round to 12632 and you shouldn't have to go through all those girations to get it to round properly.

    Hopefully MM will fix it.
    Brian Philippus Guest

  8. #8

    Default Re: Serious round() problem

    Before reporting bug, just try this first:

    <cfoutput>
    #NumberFormat(0.5053*100, "___._______________________________")#
    #Round((0.5053*100) * 250)#<br>
    #NumberFormat(50.53, "___._______________________________")# #Round(50.53 *
    250)#<br>
    </cfoutput>


    Mr Black Guest

Similar Threads

  1. #24876 [NEW]: Round does not round properly
    By jeff at tmtrading dot com in forum PHP Development
    Replies: 0
    Last Post: July 30th, 11:57 PM
  2. #24828 [Asn->Csd]: round() function doesn't round !
    By edink@php.net in forum PHP Development
    Replies: 0
    Last Post: July 28th, 10:08 PM
  3. #24828 [Opn->Asn]: round() function doesn't round !
    By iliaa@php.net in forum PHP Development
    Replies: 0
    Last Post: July 27th, 06:25 PM
  4. #24828 [NEW]: round() function doesn't round !
    By christophe dot bidaux at netcourrier dot com in forum PHP Development
    Replies: 0
    Last Post: July 27th, 11:34 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •