-1

I am trying to add double numbers which begin with 0.5,0.6... to 179.9,180.0. To do it, I used the following code;

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    for (double i = 1; i <= 180; i+=0.1) {
        listDegrees->Items->Add(i);
    }
}

but when I run the program, it shows something like this:

http://imgur.com/Ap6Tosj

How can I minimize the number only two precision?


edited:

I have a project for my school. We will do some activities with electronic circuits so that, when I choose a coordinate from program, electronic circuit will turn right/left/down or up. So that I need this.

the program should be windows application to be useful for all people. the screenshot from the program is like that; https://i.stack.imgur.com/sUHg7.png

so far all code I have is I have shared before. I guess, I have to edit ListBox1->Items->Add(i); part of my code to deal with this problem.

  • 1
    see http://programmers.stackexchange.com/questions/101163/what-causes-floating-point-rounding-errors . If precision is really important to you then consider using a decimal library. – Dunes Jul 06 '14 at 17:30
  • This is an issue of how floating point numbers are being printed. Please show the part of your code that prints the numbers. – NicholasM Jul 06 '14 at 18:01

1 Answers1

1

It's easy to find this question in google. There's a function in cout, setprecision. For more info look here http://www.cplusplus.com/reference/iomanip/setprecision/

Oleksandr Verhun
  • 814
  • 1
  • 8
  • 23
  • I have tried it. I can use this method for console applications but for windows applications, I dont know how I can use. – user3552878 Jul 06 '14 at 17:34
  • What do you use to make win app?WinForm? – Oleksandr Verhun Jul 06 '14 at 17:37
  • C++Builder uses either the Visual Component Library (aka VCL) or FireMonkey (aka FMX), both from Embarcadero, to write Windows apps. He should probably use the VCL's Format() or FormatFloat() to get a (proprietary format understood by the VCL) string that is formatted as he likes. – Rudy Velthuis Jul 06 '14 at 21:34