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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding the numeric variable (declared as double) up to n digits 1

Status
Not open for further replies.

nomy

Programmer
Jul 20, 1999
23
0
0
PK
I got problem in rounding the number declared as double in vb. I am using access database.
 
Have you tried the Format function? Example follows:<br>
<br>
dblNumber = Format(dblIn,
 
Try this function<br>
Public Function RoundToFirst(X As Single)<br>
RoundToFirst = Int(X / 10 ^ Int(Log(X) / Log(10))) * 10 ^ Int(Log(X) / Log(10))<br>
End Function<br>
<br>
Public Function RoundToSecond(X As Single)<br>
RoundToSecond = Int(X / 10 ^ (Int(Log(X) / Log(10)) - 1)) * 10 ^ (Int(Log(X) / Log(10)) - 1)<br>
End Function<br>
<br>
Public Function RoundToThird(X As Single)<br>
RoundToThird = Int(X / 10 ^ (Int(Log(X) / Log(10)) - 2)) * 10 ^ (Int(Log(X) / Log(10)) - 2)<br>
End Function<br>
<br>
Public Function RoundToFourth(X As Single)<br>
RoundToFourth = Int(X / 10 ^ (Int(Log(X) / Log(10)) - 3)) * 10 ^ (Int(Log(X) / Log(10)) - 3)<br>
End Function<br>
Public Function RoundToDigit(X As Single, SignificantDigits As Integer)<br>
RoundToDigit = Int(X / 10 ^ (Int(Log(X) / Log(10)) - (SignificantDigits - 1))) * 10 ^ (Int(Log(X) / Log(10)) - (SignificantDigits - 1))<br>
End Function<br>

 
function RoundNumber(NumberToRound as Double, Places as integer) as Double<br>
dim Mask as string<br>
Mask = &quot;000000000000000000.&quot;<br>
Mask = Mask & Left(Mask, 1, Places)<br>
RoundNumber = cdbl(Val(Format(NumberToRound,Mask)))<br>
exit function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top