+ Reply to Thread
Results 1 to 6 of 6

Bug in the Trunc function !?!

  1. #1
    Registered User
    Join Date
    06-23-2011
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Bug in the Trunc function !?!

    When trying to extract the decimal part of a number a really odd thing happens, I will try to explain the simple spreadsheet which displays this odd problem

    ........A..........B....................C..............D.................E
    ....................=trunc(A1).......=A1-B1......=C1*10........=trunc(D)
    1......22.6......22...................0.6............6..................6
    2......22.7......22...................0.7............7..................6............-obviously should be 7 Why would TRUNC(7) = 6
    3......22.8......22...................0.8............8..................8
    4......22.9......22...................0.9............9..................8............-obviously should be 9

    I want to trunc "result D" again in case someone started with a number that included more than the tenths, ie hundreths, thousandths, etc., but it behaves very oddly. If I change the ten multiplier in D to 10.1, the trunc functions correctly. To me this has to be a bug with Excel which is very strange to find in a 2007 product. Has anyone experienced this?

    The following reslults are obtained:
    0 = 0
    .1 & .2 = 1
    .3 & .4 = 3
    .5 = 5
    .6 & .7 = 6
    .8 & .9 = 8

    To my understanding the INT function always rounds down but the TRUNC function should just remove the fraction portion. Please let me know if there is something about the TRUNC function that I am unaware of.
    Last edited by Mac_Sr; 06-24-2011 at 12:24 AM.

  2. #2
    Valued Forum Contributor ron2k_1's Avatar
    Join Date
    09-30-2009
    Location
    Not the USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    606

    Re: Bug in the Trunc function !?!

    Why not use:
    =MOD(A1,1)

    Assuming that the values are on column A starting from A1. If you don't want your result in decimal then you can add this to the formula:
    =MOD(A1,1)*10
    If all your values in column A only have 1 decimal place, if you have 2 decimal places then times it by 100
    Ron
    Knowledge is knowing that a tomato and pepper are fruits. Wisdom is knowing whether to put these in a fruit salad

    Kindly

    [1] Use code tags. Place "[code]" before the first line of code and "[/code"]" after the last line of code. Exclude quotation marks
    [2] Mark your post [SOLVED] if it has been answered satisfactorily by editing your original post in advanced mode.
    [3] Thank (using the little scale) those that provided useful help; its nice and its very well appreciated

  3. #3
    Registered User
    Join Date
    06-23-2011
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Bug in the Trunc function !?!

    I need the number from the tenth place as a single integer so your idea may not work well if a person types in multiple places after the decimal point. I have found a solution to my original formula which is to multiply by 10.1 instead of ten. All number regardless of what a person types in will calculate correctly. I still find the original calculation extremely odd and believe it is an error in Excel programming.

    My final formula if you are curious is: trunc((#-trunc(#))*10.1)
    I suppose rounding the number in the beginning would add a bit more accuracy to it also.

    Thanks for the suggestion as I have never used the MOD function previously.

    Anyone else understand if my original question is due to a programming error or an explanable result for the TRUNC function?

    I have been playing a bit and found even more oddities: trunc((7.7-trunc(7.7))*10)=7 but ironically trunc((8.7-trunc(8.7))*10)=6
    Last edited by Mac_Sr; 06-24-2011 at 12:46 AM.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Bug in the Trunc function !?!

    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    06-23-2011
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Bug in the Trunc function !?!

    Thanks!

    The proof really shows through that the subtraction is the problem when you round the results in column C to get the exact same displayed numbers, yet the end result is calculated differently. To my error I had always considered these issues to only be prominent when calculating numbers well after the decimal point and not an issue when restricting oneself to single itegers and tenths. Live and Learn. This thread can be marked as solved.

  6. #6
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Bug in the Trunc function !?!

    Mac, you can mark the thread as Solved yourself.

    In the first post in the thread click the Edit button, then click Go Advanced. In the Prefix box, select Solved and then submit the post again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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