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
Solved

Proper way to round up a number and store it?

Posted on 2006-07-12
9
311 Views
Last Modified: 2012-06-21
I am dividing a number which gives me a result of, for example: 0.04655

I wish to round this numbr up to 0.05 and store it in the data field. I have set the field to SINGLE with 2 Decimal places, but the number remains as 0.04655. How can I force a rounding-up of this number and then store it as 0.05?

I am sure it is "really" simple to do....but I am brain-dead today.... :>)
0
Comment
Question by:SOTA
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 50 total points
ID: 17091195
Hi SOTA,

If you want to STORE it like that then you need to use a Round() function on the value.  Be aware that the built-in Round() function might not be quite what you are expecting so test it on a range of values.


On the other hand, if you just want to <display> the value to 2dp then you can use the format and decimal place settings wherever you display it.

Pete
0
 
LVL 25

Expert Comment

by:jrb1
ID: 17091254
Only the later versions of access have the round function.  In the earlier versions you have to:

format(0.04655,"#.##")
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17091259
I normally use format for this

format(field,"0.00")

as round(field,2) sometimes does not gvie two decimal places
e.g.

12.5

format results in 12.50
round results in 12.5

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 25

Expert Comment

by:jrb1
ID: 17091260
?format(0.04655,"#.##")
0
 

Author Comment

by:SOTA
ID: 17091476
Maybe that is why I am racking my brain! There is no Round() function in Access97, correct?

I have used Format(field,"0.00") but it only display this value. The "real" value remains as 0.04566, correct?

So, I need to find a Round() function that I can paste into my code?

Thanks!
0
 

Author Comment

by:SOTA
ID: 17091500
Ah! I just found this:

ACC: Round or Truncate Values to Intended Number of Decimals
View products that this article applies to.
Article ID : 97524
Last Review : August 15, 2005
Revision : 2.1
This article was previously published under Q97524
Moderate: Requires basic macro, coding, and interoperability skills.


On This Page

SUMMARY

MORE INFORMATION

Examples of Using the Round and Truncate Functions

Example 1

Example 2

Limitations
SUMMARY
The Format property of a control can round a Number or Currency field to the number of decimal places that you want. However, this does not change the underlying data, which may contain additional digits that the control does not display. If you add the values in this control, the sum is based on the actual values and not on the displayed values. This may make the total seem inaccurate.

This article shows you how to create four user-defined functions to round or truncate data to two decimal places so that the displayed and formatted value and the actual numeric or currency data are the same.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in Microsoft Access version 2.0
 Back to the top

MORE INFORMATION
The functions are presented in two styles. The first style is appropriate for the AfterUpdate property of a form control to ensure that the data entered matches the data that is displayed. The second style is for use in expressions and calculated controls.

To round or truncate numbers to two decimal places, create a new module and add the following functions.
   '******************************************************
   ' Declarations section of the module
   '******************************************************

   Option Explicit
   Const Factor = 100

   '=====================================================
   ' RoundAU and TruncAU are designed to be added to the
   ' AfterUpdate property on a form control.
   '=====================================================
   Function RoundAU(X As Control)

      X = Int(X * Factor + .5) / Factor

   End Function

   Function TruncAU(X As Control)
      X = Int(X * Factor) / Factor
   End Function

   '=====================================================
   ' RoundCC and TruncCC are designed to be used in
   ' expressions and calculated controls on forms and reports.
   '=====================================================
   Function RoundCC(X)

      RoundCC = Int (X * Factor + 0.5) / Factor

   End Function

   Function TruncCC(X)
      TruncCC = Int (X * Factor) / Factor
   End Function

0
 
LVL 77

Expert Comment

by:peter57r
ID: 17091535
Looks like you are on your way, now.

It's a pity this site doesn't force questions to include software versions.  It would save a lot of wasted time on both sides.

Good luck.

I think you should be able to apply for a refund, if you go to Community Support.

Pete


0
 
LVL 25

Assisted Solution

by:jrb1
jrb1 earned 50 total points
ID: 17091545
This one is more flexible...may be better for you:

http://support.microsoft.com/?kbid=209996

Function RoundToNearest(dblNumber As Double, varRoundAmount As Double, _
   Optional varUp As Variant) As Double
           
   Dim dblTemp As Double
   Dim lngTemp As Long
     
   dblTemp = dblNumber / varRoundAmount
   lngTemp = Clng(dblTemp)
     
   If lngTemp = dblTemp Then
         RoundToNearest = dblNumber
   Else
      If IsMissing(varUp) Then
         ' round down
         dblTemp = lngTemp
      Else
         ' round up
         dblTemp = lngTemp + 1
      End If
      RoundToNearest = dblTemp * varRoundAmount
   End If
End Function

• RoundToNearest(3.33, 0.1, up) returns the value 3.4.
• RoundToNearest(3.33, 0.1) returns the value 3.3.
0
 

Author Comment

by:SOTA
ID: 17091671
Thanks peter57r and jrb1 for your replies! I decided to split points as you both have helped me out.

I like this one:

Function RoundToNearest(dblNumber As Double, varRoundAmount As Double, _
   Optional varUp As Variant) As Double

Thanks Experts!!!!
Russ :>)

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

791 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