Results 1 to 9 of 9

Thread: The sloppyness of FloatToStr!

  1. #1
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    The sloppyness of FloatToStr!

    Ok take a look at the following code:
    [pascal]MrHappyString := FloatToStr(10000);
    ShowMessage(MrHappyString);[/pascal]

    Ok looks rather simple right? Well, $10 bucks says it'll return '9999.999843524425' or some other crap. :?

    For my purposes I require a simple and *accurate* conversion of a int/real value to a string with no trailling 0s or unrequired decimal point.

    Does anyone have some code suggestions to replace the FloatToStr function?
    Jason McMillen
    Pascal Game Development
    Co-Founder





  2. #2

    The sloppyness of FloatToStr!

    Have you tried the format function? I use it like this:

    [pascal]
    lblFPS.Caption := Format ('%4.0f fps', [fps]);
    [/pascal]

    eg. 28.345686797 would give you '28 fps'

  3. #3
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    The sloppyness of FloatToStr!

    Wow, fast reply.

    Ok I'll give it a try...
    Jason McMillen
    Pascal Game Development
    Co-Founder





  4. #4
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    The sloppyness of FloatToStr!

    Crap... still does it. I think it's because I'm trying to pass it as a general number... ie. [pascal]Format('%g', [RealVar]);[/pascal]

    See what I require is that if the Real holds the value of 10 then it'll return a string that says '10'. However if the Real holds a value of 25.75 then the string will be simply '25.75'. Either case, same line of code.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  5. #5
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    The sloppyness of FloatToStr!

    [pascal] label1.Caption := FormatFloat('###0.##',25.75);
    label2.Caption := FormatFloat('###0.##',10);
    label3.Caption := FormatFloat('###0.##',0.75); [/pascal]
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  6. #6

    The sloppyness of FloatToStr!

    Instead of FloatToStr, you could also try FloatToStrF

    like this:

    Code:
    label1.Caption := FloattostrF(12345.6789,fffixed,7,2);
    which results in 12345,68

  7. #7
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    The sloppyness of FloatToStr!

    cairnswm: You da man! It worked finally. I was starting to think that there was something wrong with either Delphi(Delphi 5 Standard) or my processor(PIII 800 laptop)... :?

    Traveler: Thanks I did try it, but I need to remove unwanted decimal points and 0s. Reason being is that this is for a script engine that has finished it's evaluation of a parameter and needs to return the resulting value to a tokenized symbol to be properly executed with all processed math functions.

    Off-Topic-ish :arrow: LOL This thing has been getting more and more complicated by the feature. It would seem that script engines aren't the easiest things to do. But it's a damn cool feature to a game.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  8. #8

    The sloppyness of FloatToStr!

    Keep in mind the nature of floating point numbers. For an example in base-10 -- how can you directly represent a number such as (1/3) with a finite amount of digits (remembering that those threes will keep going...). The same applies for singles/doubles in base-2 -- there are some numbers you can't represent exactly with the allocated amount of bits, so there will be some round-off error creeping in sometimes. Now, I'm not sure that this is the case here, but it may well be that your given numbers coincidentally have this problem. If you increase your precision (from singles to doubles or extended) and the problem goes away then you're fit to go.

    The above is also the same reason that you shouldn't compare two floating point numbers directly (if a = b), but should always use an epsilon instead (maybe something like (if abs(a - b) < epsilon) where epsilon is a smallish number (maybe 1e-6 or something), off the top of my head).

    Maybe the above doesn't apply in your case, though. At least you've found a solution!

    Incidentally, would the following not do what you wanted?

    [pascal]procedure TForm1.Button1Click(Sender: TObject);
    var
    str: string;
    begin
    str := Format('%4.2f', [24.342343]);
    ShowMessage(str);
    end;[/pascal]
    "All paid jobs absorb and degrade the mind."
    <br />-- Aristotle

  9. #9
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    The sloppyness of FloatToStr!

    Thanks for the post Ali, some information there.

    As for your code; Like Traveler's suggestion, it would leave me with two possibly unwanted decimal points where I may need it to be rounded off to an Integer when converted back to a string. Thanks though.
    Jason McMillen
    Pascal Game Development
    Co-Founder





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •