Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Old code needs fixing (problem solved)
#6
The problem was not using proper Sci Notation. Angry

First number must be between 1 and < 10 and you have to use + for positive powers of 10.

Code: (Select All)
Print Val("1.1E+6")
Print Val("1.01E+7")
Print Val("1.001E+8")
Print Val("1.E-6")
Print Val("1.1E-6")
Print Val("1.01E-6")
Perfect

Code: (Select All)
_Title "N2S$ Test Steve's converter for number display" ' b+ 2022-11-10

' test
Print N2S$("1.1E+6")
Print N2S$("1.01E+7")
Print N2S$("1.001E+8")
Print N2S$("1.E-6")
Print N2S$("1.1E-6")
Print N2S$("1.01E-6")


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) ' remove decimal
    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

Also fine!
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply


Messages In This Thread
Old code needs fixing (problem solved) - by bplus - 09-27-2024, 09:31 AM
RE: Old code needs fixing - by Petr - 09-27-2024, 10:23 AM
RE: Old code needs fixing - by Kernelpanic - 09-27-2024, 10:59 AM
RE: Old code needs fixing - by bplus - 09-27-2024, 11:49 AM
RE: Old code needs fixing - by Kernelpanic - 09-27-2024, 12:24 PM
RE: Old code needs fixing (problem solved) - by bplus - 09-27-2024, 02:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  code locks up when SHELL paulel 4 289 02-15-2026, 08:04 PM
Last Post: madscijr
  I'm looking for suggestions to solve the problem! Petr 10 680 02-05-2026, 04:56 PM
Last Post: ahenry3068
  Nth problem with hardware images Ikerkaz 9 498 01-23-2026, 02:58 PM
Last Post: bplus
  Install problem...... jssantoro 6 397 12-17-2025, 08:31 PM
Last Post: madscijr
  _putimage (Solved) Pete 0 224 11-16-2025, 06:48 AM
Last Post: Pete

Forum Jump:


Users browsing this thread: 1 Guest(s)