+ Reply to Thread
Results 1 to 12 of 12

Strange behavior with rounding

  1. #1
    Registered User
    Join Date
    11-28-2023
    Location
    US
    MS-Off Ver
    2021 and 365
    Posts
    14

    Strange behavior with rounding

    I have a situation where I am converting decimal feet back to separate feet, inch, and fraction numerators. I have a couple of instances were using either ROUNDDOWN or TRUNC is giving me a result of 1 less than what it should be. At first I thought I was missing something with the rounding function default behavior, but then I tried TRUNC and i get the same thing.

    Basically I took the resulting 12.41666667 of decimal feet and preformed a round down to drop the decimal places to get 12. I then isolated the inches by subtracting the cell with 12 from the cell with 12.4166667 and multiplied it by 12 to get an even 5 in decimal inch, but when preform a ROUNDDOWN(S7,0) i end up with a result of 4. TRUNCE does the same thing. It also happens with 18.333333 decimal feet but works as I intend/need it to with 24.25 decimal feet.

    So what is happening is with the 12.41666667 example I am getting a final result of 12-4-16 (that is 16 6ths of an inch) instead of 12-5-0. The 24.25 example is correctly giving me 24-3-0.

    I am sure I can come up with some crazy work around, but i would rather figure out why it is doing that in the first place.

    In the file attached I have the bad ones highlighted in red and the good one highlighted in green.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,905

    Re: Strange behavior with rounding

    Use ROUND not ROUNDOWN

    in S7, inches are 4.99999999 so ROUNDOWN(S7,0) will result in 4
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,494

    Re: Strange behavior with rounding

    If you increase the decimal places, you will discover that there is a small difference versus the displayed values.

    5.499999999999990 1/2
    11.250000000000000 1/4
    4.999999999999970 1
    10.750000000000000 3/4
    4.499999999999960 1/2
    10.250000000000000 1/4
    3.999999999999990 1
    9.750000000000000 3/4
    3.500000000000010 1/2
    9.250000000000030 1/4
    3.000000000000040 0
    8.750000000000060 3/4

    Hence, if you round down or truncate the number, and subtract that from the number, you are left with a very large fraction. See the examples above.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Registered User
    Join Date
    11-28-2023
    Location
    US
    MS-Off Ver
    2021 and 365
    Posts
    14

    Re: Strange behavior with rounding

    ROUND wont work as it gives me undesired results elsewhere, like taking 8.75 and turning it into 9 inches and -4 (/16ths).

    Weird that when I checked the calculations with my calculator I get 5.000004 but when I expand the decimal places in Excel I see where you are saying it is 4.99999999.

    Is there a way to make excel TRUNC the decimal places from what is shown instead of actually calculated? I feel like by default TRUNC should drop what is seeing, not what is "behind the scenes"
    Last edited by Venomous Duck; 12-17-2023 at 07:30 PM. Reason: typo correction

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,494

    Re: Strange behavior with rounding

    Maybe just use a formula:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,494

    Re: Strange behavior with rounding

    Or, you could just format column S as Fraction. That gives the "look" you want but retains the numeric value (in inches and fractional inches).

    5 1/2
    11 1/4
    5
    10 3/4
    4 1/2
    10 1/4
    4
    9 3/4
    3 1/2
    9 1/4
    3
    8 3/4

  7. #7
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,475

    Re: Strange behavior with rounding

    Here is another formula that will work...
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Last edited by Rick Rothstein; 12-17-2023 at 08:52 PM.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,494

    Re: Strange behavior with rounding

    Or with the quote marks - Custom Format:
    PHP Code: 
    # #/#\" 
    5 1/2"
    11 1/4"
    5"
    10 3/4"
    4 1/2"
    10 1/4"
    4"
    9 3/4"
    3 1/2"
    9 1/4"
    3"
    8 3/4"
    Last edited by TMS; 12-17-2023 at 08:50 PM.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,494

    Re: Strange behavior with rounding

    Updated sample file
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    11-28-2023
    Location
    US
    MS-Off Ver
    2021 and 365
    Posts
    14

    Re: Strange behavior with rounding

    Thanks a lot TMS. My problem with what you have in Rick's solution is it looks to be rounding up anything with a decimal value higher than .5 thus making 9-3/4" 10. I assume it will also do it to 9/16 and up.

    I wont have time to play around with until it later but was thinking, if I simply change S to fractional format will it treat the .4999999 as 5 when I ROUNDDOWN or TRUNC row S in row M? I should then still be able to subtract M from S and multiply by 16 for row O, thus eliminating the problem of 16 instead of 0 also. S can be in any format that works, but final result in row O must be in the numerator of the non-simplified fraction to the 16th.

    Or can I specify to round up if the decimal is over .9375?

    I also am not following the logic path of either formula to understand what is happening, but I can dig into that later.

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,494

    Re: Strange behavior with rounding

    I just put Rick's solution in because it was there. Nothing to do with me and I'm not sure it does what you want.

    Formatting does nothing at all to the underlying value. So, if you use TRUNC, INT, or ROUNDDOWN, you will always get 4 out of 4.9999999

  12. #12
    Registered User
    Join Date
    11-28-2023
    Location
    US
    MS-Off Ver
    2021 and 365
    Posts
    14

    Re: Strange behavior with rounding

    Thanks for the help guys. I figured out a work around, I think. I just moved the formula from M to W and O to X. Then in M used the formula =IF(X3>15,W3+1,W3) and in O used the formula =IF(X3>15,X3*0,X3) and copied them down. So far seems to work as expected. I will play with input numbers to see if I get anything erratic out of it, but I think that should work.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Strange Findlastrow behavior.
    By DECROMAX in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-16-2013, 08:03 PM
  2. Very strange text cut-off behavior
    By Ulodesk in forum Excel General
    Replies: 5
    Last Post: 03-14-2008, 05:35 PM
  3. VBA - Strange behavior
    By MVM in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-12-2006, 02:50 PM
  4. [SOLVED] Strange TAB behavior
    By m davidson in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-28-2006, 01:15 PM
  5. Excel Mac OS X - Strange Behavior
    By BrianP in forum Excel General
    Replies: 0
    Last Post: 01-01-2006, 04:10 AM
  6. Strange behavior.
    By Wiley Coyote in forum Excel General
    Replies: 7
    Last Post: 10-18-2005, 12:05 PM
  7. [SOLVED] Strange VBE Behavior
    By Jeff Robson in forum Excel General
    Replies: 4
    Last Post: 01-14-2005, 07:06 PM

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1