Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to round off an integer to 2 decimals?

Status
Not open for further replies.

drlandau

IS-IT--Management
Jul 20, 2000
36
DK
Hi!<br><br>I need to be able to round off an integer like 16.666666666667 to a specific number of decimals like 16.67.<br><br>Examples:<br>RoundOff((100/6),2) will return 16.67<br>RoundOff((100/6),0) will return 17<br>RoundOff((100/6),5) will return 16.66667<br><br>Does Access 97 have a build-in function for this?<br><br>Thanks!<br><br>/Nicolaj
 
You can set the number of decimal places when you are designing a table.<br><br>Open the table where the values are stored and then got into design mode. <br><br>You can change the number of decimal at the bottom of the screen the default is 'Auto'<br><br>hope this helps.
 
I need to do it in a field in a report, so I can't adjust the decimal-places in any table, but thanks anyway (I should have written this in the first thread).<br><br>The result should be written in a field in a report like this:<br><br>='Total items sold in percent: '+Str(RoundOff(((NumSold/NumTotal)*100),2))+'%'<br><br>And the outcome in the report should be something like this:<br><br>Total items sold in percent: 16,67%<br><br>The problem is that I can't find any build-in function like RoundOff in my example. Do I have to write a module in Access Basic myself to do it?<br><br>/Nicolaj
 
could you set the data format to currency? I think that might round it for you. You could create a custom one where it displays the % sign after the number.<br><br>Or you cold do it like this.<br>Using VBA convert the number to a string, get the lenght of the string. Store each character of the string as part of an array. Using a series of IF,THEN,ELSE and for loops or your preferred method. Look at each character of the string until you find the decimal then simply cut the inwanted decimals places of the string.<br><br>There are various methods for doing all of these commands, altough I can this code taking up some time!<br><br>Hope this helps.
 
morgion,<br><br>I have written a function to do just what you are asking.&nbsp;&nbsp;Here it is:<br><br>--------------------------------------------------------<br>Public Function roundDouble(dblValue As Double, bytPlaces As Byte) As Double<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim dblRounder As Double, lngMultiplier As Long<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;'Calculate the rounding variable<br>&nbsp;&nbsp;&nbsp;&nbsp;dblRounder = 5 / (10 ^ (bytPlaces + 1))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;'Calculate the number to mulitply by<br>&nbsp;&nbsp;&nbsp;&nbsp;lngMultiplier = 10 ^ bytPlaces<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;roundDouble = (Int((dblValue + dblRounder) * lngMultiplier)) / lngMultiplier<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>End Function<br>--------------------------------------------------------<br><br>By the way, by definition, an integer contains no decimals, which is something that bugs me about Access (how you can have decimal places for your integers).&nbsp;&nbsp;You can pass any type of value to this function, but it will return a double.&nbsp;&nbsp;Enjoy. <p>-Chopper<br><a href=mailto: > </a><br><a href= > </a><br>
 
You can set the properties of a field in the&nbsp;&nbsp;report to&nbsp;&nbsp;have only two decimal places just select the field go to properties and under all choose decimal places '2'. and that is a little simpler than a whole Function. <p>Walt III<br><a href=mailto:SAElukewl@netscape.net>SAElukewl@netscape.net</a><br><a href=
 
Also if you want to add a symbol to the print out. Go to the properties again and change the control source to <br><br>= [ control field] & &quot;whatever text you want here&quot;<br>&nbsp;<br>that way you can add text to a printout like 16.67% would be <br><br>&nbsp;= [source field] & &quot;%&quot; <p>Walt III<br><a href=mailto:SAElukewl@netscape.net>SAElukewl@netscape.net</a><br><a href=
 
Thanks all - I got it now!<br><br>I also found this snippet somewhere on the web:<br><br>--- Start of snippet ---<br>'X is the number to round off<br>'Factor is the number of decimals<br>'&nbsp;&nbsp;10 = 1 decimal<br>'&nbsp;&nbsp;100 = 2 decimals<br>'&nbsp;&nbsp;1000 = 3 decimals, and so on<br><br>Function RoundCC(X, Factor)<br>&nbsp;&nbsp;RoundCC = Int(X * Factor + 0.5) / Factor<br>End Function<br>--- End of snippet ---<br><br>Thanks again!<br><br>/Nicolaj<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top