Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scientific Notation to Decimal Value
#1
I'm interested in knowing if there might be a different way to recalculate Scientific Notation into a decimal value. Here is the method I'm presently using which works ok with Single dimension arrays/variables but when the resultant value is zero I've been going back are Redim to Double or whatever can get me a decimal value other than zero.

Code: (Select All)
' Removing Scientific Notation from a Variable/Array of Single dimension


Dim Shared Number, x, y


'In the main module will be a calculation which generates a Scientifically Notated value

Number = 115 / 123456

Print Number

x = 115
y = 123456

Number = SNrid

Print Number

Function SNrid
    ScNote$ = Str$(x / y)
    If InStr(1, ScNote$, "E") Or InStr(1, ScNote$, "D") Then
        SNA = x / y
        a = SNA * 10000000
        b = a \ 1
        C = a - b
        If C >= .5 Then
            b = b + 1
            SNB = b * .0000001
        End If
        If C < .5 Then
            SNB = b * .0000001
        End If
        SNrid = SNB
        Exit Function
    End If
    SNrid = x / y

End Function
Reply
#2
I don't understand, is it for displaying values without sci notation?
if so, then why not use Print Using ?
if what you want is a fixed-point representation of a value in a string then I would use the C sprintf function
Reply
#3
Code: (Select All)
N = 115 / 123456
Print N2S$(Str$(N))
For i = 4 To 8
    Print roundDP$(N, i)
Next


' this function needs N2S$ (next) watch number type???
Function roundDP$ (num, digits) 'mod 2022-11-10
    Dim s$, dot
    s$ = N2S$(Str$(num + (Sgn(num) * .5) * 10 ^ -digits))
    dot = InStr(s$, ".")
    If dot Then roundDP$ = Mid$(s$, 1, dot + digits) Else roundDP$ = s$
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 + ...
Reply
#4
nice use of Steve's N2S$, bplus
Reply
#5
Hi bplus .. thanks for the routine, it will likely solve my problem of a zero result. 

I see where there is a ReDim of 1 as long. How does dimensioning "1 as long"  fit into the overall scheme of determining if a result is expressed in Scientific Notation? Isn't a value of 1 by it's nature a Single? I don't think I have seen that before and wonder if it may have other applications.
Reply
#6
That's an "l" as in lucky not a 1 as in "one thing". Smile
b = b + ...
Reply




Users browsing this thread: 1 Guest(s)