UnderwareDESIGN

PlayBASIC => Beginners => Topic started by: stevmjon on July 13, 2022, 04:18:46 AM

Title: convert small value floats into strings?
Post by: stevmjon on July 13, 2022, 04:18:46 AM
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?
Title: Re: convert small value floats into strings?
Post by: kevin on July 13, 2022, 05:31:01 AM
  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



Title: Re: convert small value floats into strings?
Post by: stevmjon on July 14, 2022, 12:43:07 AM
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?

[pbcode]

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

[/pbcode]
Title: Re: convert small value floats into strings?
Post by: kevin on July 22, 2022, 07:32:21 AM
   Made an alternative STR$() function that removes the scientific notation.


[pbcode]


   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$



[/pbcode]


  Learn to code: What does STR$() do ? (http://www.playbasic.com/help.php?page=STRINGS.STR$)