Results 1 to 3 of 3
  1. #1
    lccrews is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Location
    Los Angeles
    Posts
    194

    Problem with rounding code

    I'm using the below script to take numbers from my table and round them based on the value. If the number is above 15, I want the value to round off to one decimal place. If the number is below 15, I want the value to round off to three decimal places. The formula is working on numbers above 15, but will not place a ".0" next to whole values. I suspect that Access will not do this. Am I right? If there is a better way to do this please let me know.



    SELECT Tbl_Formulas.QtyPer, Round([QtyPer],IIf([QtyPer]<=15,3,1)) AS Expr1 FROM Tbl_Formulas;

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    The issue is because for numeric values, decimals of ".0" have no value or meaning and can be dropped.
    If you change the result to Text, you can keep them, like this:
    Code:
    SELECT Tbl_Formulas.QtyPer, IIf([QtyPer]<=15,Format(Round([QtyPer],3),"0.000"),Format(Round([QtyPer],1),"0.0")) AS Expr1
    FROM Tbl_Formulas;

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    place this code into a module, and the query (or form field) will return your format
    Ex: select Found15([field]) from table

    Code:
    Function Round15(ByVal pvFld)
    If pvFld > 15 Then
      If (pvFld - Int(pvFld)) = 0 Then
         Round15 = Format(pvFld, "00")
      Else
         Round15 = Format(pvFld, "0.0")
      End If
    Else
      Round15 = Format(pvFld, "0.000")
    End If
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 12
    Last Post: 08-23-2016, 09:22 AM
  2. compare two values rounding problem
    By BCJourney in forum Queries
    Replies: 7
    Last Post: 08-06-2013, 07:27 AM
  3. Rounding Problem With Percentages
    By Lady_Jane in forum Queries
    Replies: 5
    Last Post: 09-01-2011, 02:32 PM
  4. Rounding problem
    By chavez_sea in forum Access
    Replies: 6
    Last Post: 03-09-2011, 09:01 PM
  5. Rounding problem
    By jgelpi16 in forum Queries
    Replies: 1
    Last Post: 04-06-2010, 10:27 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums