Delphi - Str function: Double type with 12 decimals results in wrong string

Posted on 2009-02-11
Last Modified: 2013-11-23
I'm using the Str function in Delphi 6 (to be able to ignore locale differences and make sure the decimal separator is always period) and when I have a Double type variable and set the Decimals indicator to 12, I can get strange results. If the Double type variable holds the value 270000 (Frac = 0), the call to Str results in a string like '270000.000000000032', i.e. it for some reason adds some decimals to the very end. Can this be a problem with floating point accuracy or something similar, and what would be a good solution to this? However, this seems to be tied to perhaps something else that has been done with the variable, since when I try to create a reduced code like the one attached, this error does not occur. In my actual code the ActVal comes from other variables and has had a life of its own before, so something in there is probably causing this, but the end result still is, that the debugger shows the value as 270000 without decimals and even a (Frac(ActVal) = 0) test evaluates to true, and still the resulting S ends with 32.

  S: string;

  ActVal : Double;


    ActVal := 270000;



Open in new window

Question by:FALECoder
    LVL 84

    Expert Comment

    LVL 4

    Expert Comment

    You can use:

    S := FormatFloat('0.000000000000',ActVal);

    This function was always working fine for me
    LVL 4

    Accepted Solution

    Sorry my mistake. FormatFloat will use separator defined in DecimalSeparator variable. To ensure it will be periond, using FormatFloat you have to use StringReplace too:

    s := StringReplace(FormatFloat('0.000000000000',ActVal),DecimalSeparator,'.',[rfReplaceAll]);

    Author Comment

    Thanks dprochownik, I guess that could be a good way to fix this. I haven't yet tried if it works in my situation (I made a more crude solution that works also in my case and don't have time just now to try this also, but I'll keep this in mind), but I would think it should work ok.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
    Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now