Results 1 to 16 of 16

Thread: Round Down in Access

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Question Round Down in Access

    Everytime I enter currency in a currency textbox. It automatically rounds up. For instance, $172.845 will round to $172.85. How do I make it round down instead? Anyone know?

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Round Down in Access

    u need to calc it on your own

    offhand the only way I can remember how to do this is actually turn the number into a string then back

    (FYI: The actual value is still 172.845 but access rounds up for display purposes only. when the number is used for calcs or called back up.. it is in its original form with as many decimals as it had originally)

    VB Code:
    1. Dim NUM As Currency
    2. Dim TMP As Double
    3.  
    4. TMP = 172.845
    5. NUM = CCur(Left(CStr(tmp),InStr(CStr(tmp),".")+2))
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Round Down in Access

    Can you send me an example. It's driving me nuts!

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Round Down in Access

    If this is in an Access form textbox then just set the Decimal Places property to 3 or more depending on the accuracy you desire.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Round Down in Access

    The table below the form will also need to be changed as the issue could be at this point.. (InputMask, Format for example)
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Round Down in Access

    It is an Access form textbox. I want two decimals. But it's rounding 43.455 to 43.46 and even if the number is 43.455 I want it to truncate and show 43.45

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Round Down in Access

    Quote Originally Posted by dannymking
    The table below the form will also need to be changed as the issue could be at this point.. (InputMask, Format for example)
    Currency format with no input mask

  8. #8
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Round Down in Access

    Check your underlying table to see what the overall properties are for the field to which this text box is bound.

    If no joy there then you will have to use vba code to capture the value as it is entered into the textbox (either through record navigation or user interactio) and display it as a string instead. This way you can keep the two decimal places and lose the additional.. But it is bad design and will ultimately lead to further problems.
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Round Down in Access

    There is also a Format property for the textbox which will allow you to create a custom display format truncating the third or more decimal place without rounding.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Round Down in Access

    Cross post..

    change the currency to number with field size of single instead.
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Round Down in Access

    Good idea Danny. That will retain the precision of the original number. Then you can format it however you want without it rounding at all.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Round Down in Access

    Quote Originally Posted by RobDog888
    Good idea Danny. That will retain the precision of the original number. Then you can format it however you want without it rounding at all.
    So now the original number stays the same, great. But how do I format it to just show 45.26 instead of showing 45.26745674??

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Round Down in Access

    How do I use this: I tried but can't get it working. Sounds like what I want to do. I want to ignore any numbers entered after the 2 decimal places.

    To round or truncate numbers to two decimal places, create a new module and add the following functions.
    VB Code:
    1. '******************************************************
    2.    ' Declarations section of the module
    3.    '******************************************************
    4.  
    5.    Option Explicit
    6.    Const Factor = 100
    7.  
    8.    '=====================================================
    9.    ' TruncAU is designed to be added to the
    10.    ' AfterUpdate property on a form control.
    11.    '=====================================================
    12.    Function TruncAU(X As Control)
    13.       X = Int(X * Factor) / Factor
    14.    End Function
    15.  
    16.    '=====================================================
    17.    ' RoundCC and TruncCC are designed to be used in
    18.    ' expressions and calculated controls on forms and reports.
    19.    '=====================================================
    20.    Function RoundCC(X)
    21.  
    22.       RoundCC = Int (X * Factor + 0.5) / Factor
    23.  
    24.    End Function
    25.  
    26.    Function TruncCC(X)
    27.       TruncCC = Int (X * Factor) / Factor
    28.    End Function

    Examples of Using the Round and Truncate Functions

    Example 1
    Use the TruncAU() function to the AfterUpdate property of a form:
    1. Open the database
    2. Create a new module called Rounding, and type the procedures in the preceding section.
    3. Open your form in Design view, and add the TruncAU() function to the AfterUpdate property of the field (NUM)
    Form: Form1
    --------------
    VB Code:
    1. Control Name: NUM
    2.       AfterUpdate: =TruncAU([NUM])

    If a user accidentally enters $23.055 instead of $23.05, the TruncAu() function catches the mistake and changes the value to $23.05.
    Last edited by vonoventwin; Feb 8th, 2006 at 06:38 AM.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Round Down in Access

    This is exactly what I want to do Truncate. I don't want the extra numbers typed in. just the first 2 after the decimal (ex: 45.6577758, I want 45.65)

    TRUNCATE

    (v.) To cut off the end of something. Usually, the term is used to describe a type of rounding of floating-point numbers. For example, if there are too few spaces for a long floating-point number, a program may truncate the number by lopping off the decimal digits that do not fit: 3.14126 might be truncated to 3.14. Note that truncation always rounds the number down. If the number 1.19999 is truncated to one decimal digit, it becomes 1.1, not 1.2.

  15. #15
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Round Down in Access

    Code:
    strVar="45.553225" 'or variable of number
    debug.print left(strVar,instr(1,strVar,".")+2)

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  16. #16
    New Member
    Join Date
    Mar 2006
    Location
    Tenerife
    Posts
    2

    Re: Round Down in Access

    This works for me:

    Code:
    Function RoundDown(dVal As Double, Decimals As Integer) As Double
    Dim Factor As Double, fVal As Double
    Dim iVal As Integer
        Factor = Exp(Log(10) * Decimals)
        fVal = dVal * Factor
        iVal = Int(fVal)
        RoundDown = iVal / Factor
    End Function
    Use it in an AfterUpdate event.
    There are two kinds of person, the ones that think there are two kinds of person, and the ones that know better than that

Posting Permissions

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



Click Here to Expand Forum to Full Width