?
Solved

Round numbers in reports

Posted on 2013-01-30
14
Medium Priority
?
511 Views
Last Modified: 2013-02-04
I have a report for lab results that I need to round numbers for.  If a sample is positive for a virus for example and 4,452,000 are found, the number on the report should be >4,000,000.  (Greater than 4 million) I also need to round down to thousands or hundreds if that is what the result is (ie: greater than one thousand) .  Is there a function that will do this or will I need to write some code to test the number length and round down accordingly?  Any number less than 100 can be left as is.
Thanks!
0
Comment
Question by:esbyrt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 26

Expert Comment

by:jerryb30
ID: 38837820
Just millions, thousands or hundreds, or each power of ten?
0
 
LVL 25

Expert Comment

by:chaau
ID: 38837837
it is very easy to implement:
To round to 1000 use this:

Round(num / 1000) * 1000

Open in new window


So, if you want to make a universal expression, use this:

Iif(num > 1000000, Round(num / 1000000) * 1000000, Iif(num > 1000, Round(num / 1000) * 1000, num))

Open in new window

0
 

Author Comment

by:esbyrt
ID: 38837890
For the Iif statement, where do I put that?  The TotalNumber text field in the report (the one I want rounded) has a control source of TotalNumber in the qryCustomerResults.  Is there a place I can enter that in the report or does it need to go into the underlying query somewhere?
Thanks
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:esbyrt
ID: 38837896
jerryb30 - I need to round to 100s, 1000s, 10000s, 100000s, etc on up to say 100 million just to cover all the bases.
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 38837908
This works as far as rounding, not as far as the text expression

Function roundNumber(num As Long) As String
Dim i As Integer
For i = 9 To 2 Step -1
If num / 10 ^ i >= 1 Then
roundNumber = ">" & (Int(num / 10 ^ i)) * 10 ^ i
Exit For
Else: roundNumber = num
End If
Next i
End Function

select RoundNumber(somenumberField) from yourtable
0
 
LVL 25

Expert Comment

by:chaau
ID: 38837919
regarding usage the function in reports, you need to do this:

In the Control Source type: =fncFormatNumber([ColumnThatNeedsToBeFormatted])
Example of Access report
Then you can put all the code into the function. This way it is easier to maintain, and can be reused for other columns
0
 
LVL 51

Accepted Solution

by:
Gustav Brock earned 2000 total points
ID: 38838755
You can use the generic function below.

Then, to obtain the rounded value:
=RoundSignificantCurrency([YourValueField],1)

or, to have a leading bracket and return a formatted string:

=IIf(RoundSignificantCurrency([YourValueField],1)<[YourValueField],">","") & Format(RoundSignificantCurrency([YourValueField],1),"Standard")

or, to leave values <100 as is:

=IIf([YourValueField]<100,[YourValueField],RoundSignificantCurrency([YourValueField],1)

Public Function RoundSignificantCurrency( _
  ByVal curValue As Currency, _
  ByVal bytSignificantDigits As Byte, _
  Optional ByVal booInteger As Boolean) _
  As Currency

' Rounds curValue to bytSignificantDigits digits.
'
' Performs no rounding if bytSignificantDigits is zero.
' Rounds to integer if booInteger is True.
'
' Rounds correctly curValue until max/min Value of currency type multiplied with
' 10 raised to the power of (the number of digits of the index of curValue) minus
' bytSignificantDigits.
' This equals roughly +/-922 * 10 ^ 12 for any Value of bytSignificantDigits.
'
' Requires:
'   Function Log10.
'
' 2001-10-19. Cactus Data ApS, CPH.
' 2002-04-02. Added CDec() for correcting bit errors of reals.
' 2007-04-18. Int replaced with Fix to round negative values correctly.
'             Parameter booInteger made Optional.

  Dim dblTmp    As Double
  Dim dblFactor As Double
  Dim dblPower  As Double
  
  ' No special error handling.
  On Error Resume Next
  
  If bytSignificantDigits = 0 Or curValue = 0 Then
    ' Nothing to do.
  Else
    dblPower = Int(Log10(Abs(curValue))) + 1 - bytSignificantDigits
    If booInteger = True Then
      ' No decimals.
      If dblPower < 0 Then
        dblPower = 0
      End If
    End If
    dblFactor = 10 ^ dblPower
    dblTmp = curValue / dblFactor
    dblTmp = Fix(dblTmp + Sgn(dblTmp) / 2)
    ' Apply CDec() to correct for possible bit error when multiplying reals.
    curValue = CDec(dblTmp * dblFactor)
  End If
  
  RoundSignificantCurrency = curValue

End Function


Public Function Log10( _
  ByVal dblValue As Double) _
  As Double

' Returns Log 10 of input dblValue.

  ' No error handling as this should be handled
  ' outside this function.
  '
  ' Example:
  '
  '   If dblMyValue > 0 then
  '     dblLogMyValue = Log10(dblMyValue)
  '   Else
  '     ' Do something else ...
  '   End If

  Log10 = Log(dblValue) / Log(10)

End Function

Open in new window

/gustav
0
 

Author Comment

by:esbyrt
ID: 38841878
Hi folks.  I have tried both functions that have been suggested and set the control source for the text box on the report to the function plus field as shown.  Both ways I get a #Type! error in the field when the report runs.  I tried using the name of the text box in the control and the full name of the query field it's based on with the same results.  Any ideas?
Thanks for the help!
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 38841909
What is datatype of the source field?
Does the function work when used in a query (which could be used as control source in the report)?
0
 

Author Comment

by:esbyrt
ID: 38842002
The TotalNumber field in the table is a long integer.  Where would I put the function in the query to test it?
Thanks!
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 38842153
Select RoundNumber(TotalNumber) from yourTable
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 38842912
[YourValueField] must be the fieldname from the query.
The textbox must be named differently from this.

A Long cannot cause a type failure when passed to a parameter of data type Currency.
However, it will happen if some records contain Null.

/gustav
0
 

Author Closing Comment

by:esbyrt
ID: 38852574
It took me a while to follow the logic through but I finally got it.  Thanks so much for the solution!
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 38852585
Great. You are welcome!

/gustav
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question