Shop OBEX P1 Docs P2 Docs Learn Events
Convert a Float to a string — Parallax Forums

Convert a Float to a string

CountMurphyCountMurphy Posts: 19
edited 2013-02-18 15:12 in Propeller 1
I am having what I hope is a very simple issue to work out. I have a couple functions what are doing some float math, I would like to output the results of said functions onto my computer's serial terminal. To do so I am using the FloatString Object to convert one of my floats to a string. If I have a float of 1357.4999
serial.str(fs.FloatToString(1357.4999))
prints out "1357.5".

Even if I call the SetPrecision function, my output is being incorrectly rounded. Is there a way to convert my exact float to a string? Coming from a c++ c# world, this has been very frustrating. I think I've spent more time trying to cast variables then writting code for my project. Any help would be greatly appreicated.

Comments

  • lonesocklonesock Posts: 917
    edited 2013-02-18 14:41
    Not really. The floating point format only has 24 bits of precision (23, plus the leading 1), which only nets you 7 decimal places. It's the compiler casting your number to the floating point format that is losing precision.

    Jonathan
  • CountMurphyCountMurphy Posts: 19
    edited 2013-02-18 14:57
    Ok, this isn't that bad. My float formats are always ####.####. I might be able to get away with using ints for the math. Thanks for help :)
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-02-18 15:12
    If you decide to use integer math and you use a muliplier to move the decimal portion to up into the integer portion, you can use this method to display the integer with a decimal point added to the appropriate location.
Sign In or Register to comment.