Thread: Set Precision in C# Builder

  1. #1
    Registered User
    Join Date
    Nov 2017

    Set Precision in C# Builder


    I've written a program in C++ Builder 6, in order to generate a code for PSpice software. In this code I've used a for loop like this:

    for (double i=deltax/2; i<Lx; i=i+deltax)
    Memo1->Lines->Add("** i= " + (AnsiString)i);

    deltax and Lx are doubles. My problem is that after some iterations, the value of i increases by a very small extent and this small error is troublesome for me. for example when I expect i=12.08, it says i=12.08000000000001.

    As a solution, I want i to have only 2 digits as decimals. How can I write this?

  2. #2
    Registered User
    Join Date
    Jun 2017

  3. #3
    Join Date
    Aug 2017
    Question looks verbatim to the answers question.

  4. #4
    Registered User
    Join Date
    Nov 2017
    Here is the solution;

    solution 1... work in integers as if *100. Insert the decimal with a custom print routine (cout << value/100 << "." << value % 100. This keeps your precision pristine internally, so you don't accumulate roundoff errors (important if this is money, for example).

    solution 2: just change the print statement. You may want to bump round it (what if you had gotten 12.079999999999999999 instead? you need to bump it up to 12.08 ) first. That looks like std::cout << std::setprecision(2) << value << '\n'; or
    if value >= 0
    std::cout << std::setprecision(2) << value + 0.001 << '\n';
    std::cout << std::setprecision(2) << value - 0.001 << '\n';

    [COLOR=#000000][FONT=verdana]if I did all that correctly. Hopefully you get the idea.
    Last edited by Salem; 11-12-2017 at 02:25 AM. Reason: spammy links deleted

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. precision in f
    By -EquinoX- in forum C Programming
    Replies: 1
    Last Post: 02-04-2008, 11:16 AM
  2. THE END - Borland C++ Builder, Delphi, J Builder?
    By Davros in forum A Brief History of
    Replies: 5
    Last Post: 02-28-2006, 11:23 PM
  3. Precision
    By acc988 in forum C++ Programming
    Replies: 8
    Last Post: 07-26-2005, 06:17 PM
  4. set precision?
    By Geo-Fry in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2003, 10:03 PM
  5. Set Precision Help !
    By Halo in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 10:24 AM

Tags for this Thread