float error

I have code:

int nnn = 0;
float fff = 0.02; 
 
fff = fff * 10.0;  //fff = 0.2;
fff = fff * 10.0;  //fff = 2.0;
nnn= fff;         //nnn = 2;

Serial.println(nnn);  //why? nnn =1;   ????

help me! Thanks!

That code does not compile, so can't print anything.

(nnn is.probably about 1.999)

void setup()
{
  Serial.begin(115200);
  int nnn = 0;
  float fff = 0.02;
  Serial.println(fff, 7);
  fff = fff * 10.0;  //fff = 0.2;
  Serial.println(fff, 7);
  fff = fff * 10.0;  //fff = 2.0;
  Serial.println(fff, 7);
  nnn = fff;         //nnn = 2;
  Serial.println(nnn);  //why? nnn =1;   ????
}

void loop()
{
}

The output is

0.0200000
0.2000000
1.9999999
1

Arduino - Cast Casting Float to Int (Existing thread)

EDIT : Fixed cast link

Thanks for the reply! I found a solution.

int nnn = 0; float fff = 0.02;

fff = fff * 10.0; //fff = 0.2; fff= round(fff *10000.0)/10000.0; fff = fff * 10.0; //fff = 2.0; fff= round(fff *10000.0)/10000.0; nnn= fff; //nnn = 2;

Serial.println(nnn); //The output is: 2

I found a solution.

And then made it much more complicated than it needed to be.

  nnn = round(fff);

You only understand part of the problem.

vanlamtk: You only understand part of the problem.

You only asked part of the problem.