Quote:Could you explain about the no Type worries?
OK I meant Round$ could do it's magic on any variable Type but it turns out when I was testing my claim I had to make AnyNumber a _Float to preserve as many digits of the original number value of the Type possible with resorting to string math. The default Single Type that was in there in code above this post was only good for holding 7-8 digits.
Single is only good for 7-8 digits, Double maybe 14-15, maybe _Float probably gets the max possible, though _Integer64 is doing really nice in my GUI Adding Machine with a pseudo-currency type.
Anyway here are 9 Types thrown at Round$ and results look OK now that I've weaved in and out of digit limits and range limits of the Types attempting the same number in all:
Code: (Select All)
Dim t1 As Integer, t2 As _Unsigned Integer, t3 As Long, t4 As _Unsigned Long
Dim t5 As _Integer64, t6 As _Unsigned _Integer64, t7 As Single, t8 As Double, t9 As _Float
t1 = 32155.55555555
t2 = 32155.55555555
t3 = 32155.55555555
t4 = 32155.55555555
t5 = 32155.55555555
t6 = 32155.55555555
t7 = 32155.55555555
t8 = 32155.55555555
t9 = 32155.55555555
Print 1, t1, Round$(t1, 4)
Print 2, t2, Round$(t2, 3)
Print 3, t3, Round$(t3, 2)
Print 4, t4, Round$(t4, 1)
Print 5, t5, Round$(t5, 0)
Print 6, t6, Round$(t6, 0)
Print 7, t7, Round$(t7, -1)
Print 8, t8, Round$(t8, -2)
Print 8, t8, Round$(t8, -3)
Print 8, t8, Round$(t8, -4)
Print 9, t9, Round$(t9, -5)
Print 9, t9, Round$(t9, -6)
Function Round$ (anyNumber As _Float, dp As Long)
' 5 and up at decimal place dp+1 > +1 at decimal place 4 and down > +0 at dp
'2 1 0.-1 -2 -3 -4 ... pick dp like this for this Round$ Function
sn$ = N2S$(Str$(anyNumber + .5 * 10 ^ dp)) 'get rid of sci notation, steve trims it so next find dot
dot = InStr(sn$, ".")
If dot Then
predot = dot - 1
postdot = Len(sn$) - (dot + 1)
Else
predot = Len(sn$)
postdot = 0
End If
' xxx.yyyyyy dp = -2
' ^ dp
If dp >= 0 Then
Rtn$ = Mid$(sn$, 1, predot - dp) + String$(dp, "0")
Else
Rtn$ = Mid$(sn$, 1, predot) + "." + Mid$(sn$, dot + 1, -dp)
End If
If Rtn$ = "" Then Round$ = "0" Else Round$ = Rtn$
End Function
Function N2S$ (EXP$) 'remove scientific Notation to String (~40 LOC)
'SMcNeill Jan 7, 2020 ref: https://www.qb64.org/forum/index.php?topic=1555.msg112989#msg112989
'Last Function in code marked Best Answer (removed debug comments and blank lines added these 2 lines.)
ReDim t$, sign$, l$, r$, r&&
ReDim dp As Long, dm As Long, ep As Long, em As Long, check1 As Long, l As Long, i As Long
t$ = LTrim$(RTrim$(EXP$))
If Left$(t$, 1) = "-" Or Left$(t$, 1) = "N" Then sign$ = "-": t$ = Mid$(t$, 2)
dp = InStr(t$, "D+"): dm = InStr(t$, "D-")
ep = InStr(t$, "E+"): em = InStr(t$, "E-")
check1 = Sgn(dp) + Sgn(dm) + Sgn(ep) + Sgn(em)
If check1 < 1 Or check1 > 1 Then N2S = _Trim$(EXP$): Exit Function 'If no scientic notation is found, or if we find more than 1 type, it's not SN!
Select Case l 'l now tells us where the SN starts at.
Case Is < dp: l = dp
Case Is < dm: l = dm
Case Is < ep: l = ep
Case Is < em: l = em
End Select
l$ = Left$(t$, l - 1) 'The left of the SN
r$ = Mid$(t$, l + 1): r&& = Val(r$) 'The right of the SN, turned into a workable long
If InStr(l$, ".") Then 'Location of the decimal, if any
If r&& > 0 Then
r&& = r&& - Len(l$) + 2
Else
r&& = r&& + 1
End If
l$ = Left$(l$, 1) + Mid$(l$, 3)
End If
Select Case r&&
Case 0 'what the heck? We solved it already?
'l$ = l$
Case Is < 0
For i = 1 To -r&&
l$ = "0" + l$
Next
l$ = "." + l$
Case Else
For i = 1 To r&&
l$ = l$ + "0"
Next
l$ = l$
End Select
N2S$ = sign$ + l$
End Function
b = b + ...