Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rounding numbers and converting to string (hiding scientific notation)
#8
(07-25-2022, 09:09 PM)bplus Wrote:
(07-25-2022, 06:18 PM)madscijr Wrote:
(07-18-2022, 07:16 PM)bplus Wrote: Here is a Round$ that acts the way you'd expect in under 100 LOC

Finally getting around to this... thanks!

One question about part of the code:
(07-18-2022, 07:16 PM)bplus Wrote:
Code: (Select All)
Function N2S$ (EXP$) 'remove scientific Notation to String (~40 LOC)
    ...
    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
    ...

Why ReDim instead of Dim?
I only ever use ReDim if I need to grow or shrink an array. 
But these don't seem to be arrays and are not ReDimmed later in the function, so is there any advantage for ReDim that maybe I don't know?
(I figure I might as well ask!)
Thanks again!

Good question,
According to Luke ( a developer of QB64.exe) and Steve or others may confirm, ReDim can be used anywhere you use DIM. Dim is the only command you want to use to make a Static array (cleared with Erase) unless you use Static itself.

So I modified that code (that came from Steve BTW) with REDIM's probably to make Option _Explicit happy back in the period when I was using REDIM for everything except Static Arrays. 

What I like about this is no Type worries for the number!

Thanks for your reply! 
I am feeling a little slow today... coffee never kicked in this morning... been a long week, etc... 
Could you explain about the no Type worries? 

How does that work? Because I am wondering if that might be the answer to the problem where without variant types and function overloading in QB64, I need to create multiple functions to do the same thing for every type I want to support. 

For example, see the below code, an attempt to create cstr like in VB6 / VBA / VBScript. 
Because if we just have the first function, QB64 throws errors when we try to pass in Long, Double, Single, _Float, etc. - it only supports Int.
I would have to create separate functions for each type that I wanted to support - all that just so I can type "cstr$(" instead of "_Trim$(Str$("! 
And then I have to remember all the variations of the function names - not just cstr$ but also cstrl$, cstrs$, cstrul$, etc. 
Not even close to being worth it, unless we are Don Quixote! 
If there is a way to write one function that works with all types, that would make a world of difference!

Code: (Select All)
' /////////////////////////////////////////////////////////////////////////////
' Convert a value to string and trim it (because normal Str$ adds spaces)

Function cstr$ (myValue)
    cstr$ = _Trim$(Str$(myValue))
End Function ' cstr$

' /////////////////////////////////////////////////////////////////////////////
' Convert a Long value to string and trim it (because normal Str$ adds spaces)

Function cstrl$ (myValue As Long)
    cstrl$ = _Trim$(Str$(myValue))
End Function ' cstrl$

' /////////////////////////////////////////////////////////////////////////////
' Convert a Single value to string and trim it (because normal Str$ adds spaces)

Function cstrs$ (myValue As Single)
    ''cstr$ = LTRIM$(RTRIM$(STR$(myValue)))
    cstrs$ = _Trim$(Str$(myValue))
End Function ' cstrs$

' /////////////////////////////////////////////////////////////////////////////
' Convert an unsigned Long value to string and trim it (because normal Str$ adds spaces)

Function cstrul$ (myValue As _Unsigned Long)
    cstrul$ = _Trim$(Str$(myValue))
End Function ' cstrul$

etc.
etc.
Reply


Messages In This Thread
RE: rounding numbers and converting to string (hiding scientific notation) - by madscijr - 07-29-2022, 05:24 PM



Users browsing this thread: 1 Guest(s)