convert small value floats into strings?

Started by stevmjon, July 13, 2022, 04:18:46 AM

Previous topic - Next topic

stevmjon

if i have a small value float like 0.254936e-002 , when i convert this into a string$ it comes out as the same...

what i want is to convert it so it says 0.00254936

do i need to search this string$ for the 'e' then convert it manually?
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

#1
  yeah if you wanted anything other than default formatting for float-> string conversions,  then you'd have to roll something along those lines.  


  Those conversion functions need a format flag..   Or perhaps get Digits$() to do it.  or both




stevmjon

i managed to get it to work.
i am working on an editor for my 3D ray tracer. this is what i want the full float value, for display purposes.

am i going about this in the right way?

PlayBASIC Code: [Select]
value# = 0.00012345
print "numeric value = " + str$(value#)

s$ = str$(value#)
print "string value = " + s$
print ""


;--- display full length float for display purpose ---
;--- if float is very small, change value to remove e-00n ---

; NOTE: use s$

pos = instring(s$,".",1,0) ; find decimal point
pos2 = instring(s$,"e",1,0) ; check if e is in float value (represents a small value)

if pos2 ; if 'e' is in float value then modify value to full length and remove this

l$ = mid$(s$,pos2+2,3) ; grab all letters after 'e-' (only want numeric)

n = val(l$) ; convert string into a value (integer)
print "need to move decimal point = " + str$(n) + " places"

s$ = cutright$(s$,pos2) ; remove everything after 'e'
print "remove everything after 'e-' = " + s$

add = 0 ; reset
f$ = mid$(s$,1,1) ; get first letter (see if it is a minus sign or value)
if f$ = "-" then add = 1 ; if minus sign then start after this
print "start pos to insert zero's = " + str$(add) + " *this allows for a minus sign*"

s$ = replace$(s$,".","") ; remove decimal point
print "remove original decimal point = " + s$

s$ = insert$(s$,"0.",add) ; insert zero and decimal point at start

for lp = 2+add to 2+add+(n-pos) ; inset 0 and move decimal point to full length value
s$ = insert$(s$,"0",lp)
next lp
print ""
print "insert zero's and move decimal point = " + s$

endif


sync : flushkeys : waitkey



It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

#3
   Made an alternative STR$() function that removes the scientific notation.


PlayBASIC Code: [Select]
   While Value$<>"END"

value$ =readdata$()
value# = val#(Value$)
print Value#
print Str2$(Value#)
print "------------------------------"

endwhile



sync
waitkey

data "0.00012345", "-0.00012345"
data "END"



function Str2$(Value#)

Result$=str$(Value#)
local DotPos = instring(Result$,".")
if DotPOS
local Epos = instring(Result$,"e",DotPOS)
local ShiftPlaces = val(mid$(Result$,Epos+2,3))

// Cut off the characters from e characters
Result$ = cutright$(Result$,Epos)

// Check for a minus sign
local MinusState = mid(Result$,1)=asc("-")

// grag the characters from the between the minus and dot
local Result2$ = mid$(Result$,1+MinusState,DotPOS-(1+MinusState))

// grab the characters after the dot
Result2$ += mid$(Result$,DotPOS+1, (Epos-DotPOS)-1)

// get the minus character (if it existed)
Result$=left$(Result$,MinusState)

// insert the zero padding
Result$+="0."+digits$(0,ShiftPLaces-1)+Result2$

endif

EndFunction Result$







  Learn to code: What does STR$() do ?