Shop OBEX P1 Docs P2 Docs Learn Events
How to set the string of float value which are the same precision? — Parallax Forums

How to set the string of float value which are the same precision?

kevinspacekevinspace Posts: 56
edited 2011-08-01 06:32 in Propeller 1
Hello everyone~
I used the FloatString Object to display the float value, just like the code :
CON
    _clkmode = xtal1 + pll16x                           
    _xinfreq = 5_000_000                                
    
OBJ
    Debug: "FullDuplexSerialPlus"
    FMath : "F32"
    FString : "FloatString"
 
VAR long A, B, C, D, E
 
PUB Operate
    FMath.Start
     
    Debug.start(31, 30, 0, 9600)  
    Debug.tx(16)
   
    A := FMath.FFloat(10)
    B := FMath.FFloat(5)                  
    C := FMath.FFloat(3)
    D := FMath.FDiv(A, B)
    E := FMath.FDiv(A, C)
    
repeat    
    Debug.str(string("D: ", 13))       
    Debug.str(fstring.FloatToString(D))
    Debug.str(string(13))     
    Debug.str(string("E: ", 13))       
    Debug.str(fstring.FloatToString(E))
    Debug.str(string(13))
    waitcnt(clkfreq*3 + cnt)                                                                        
The result of the Parallax Serial Terminal
D: 2
K: 3.333333
My question is that how to display the D: 2.000000 and K: 3.333333, In other words, I hope all the string of float value which are the same precision.

Are there any example or any suggesstion?
Thanks a lot.

QuestionOfFloat.rar

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2011-08-01 06:32
    You could use the cfloatstr object from CLIB. I have attached it below along with a test program that prints 2.000000 and 3.333333. The putfloatf method requires a pointer to a string buffer, the value to print, the desired number of characters to print and the number of fractional bits. It converts the floating point number to an ASCII string, which can then be printed out. It doesn't even require the use of a floating point math object.
Sign In or Register to comment.