Why these strange results in serial monitor

I wrote this simplified code while trying to understand the use of decimals with analog input in my main program, as I've done little with the UNO analog pins until now.

I've plainly misunderstood how to specify the number of decimals when printing debug info. I thought a comma followed by the number of decimal places required? Where am I going wrong please?

const byte readingPin = A5;

void setup()
{
  Serial.begin(115200);
  delay(300);

  int reading = analogRead(readingPin);  // read the value from a 10K pot
  Serial.println("====================");
  //  Serial.print("reading = ");
  //  Serial.println(reading); // Prints 282 (correct)
  //  Serial.println(reading, 0); // Prints  (unprintable, small square)
  //  Serial.println(reading, 1); // Prints 282 (correct)
  //  Serial.println(reading, 2); // prints 100011010
  //  Serial.println(reading, 3); // prints 101110

  // So I tried more examples:
  for (byte i = 0; i <= 20; i++)
  {
    Serial.print("Serial.println(reading, ");
    Serial.print(i);
    Serial.print("); prints ");
    Serial.println(reading, i);
  }
  while (1 == 1); // Stops program from proceeding
}

void loop()
{
}

/*
 RESULTS:
 
  Serial.println(reading, 0); prints 
  Serial.println(reading, 1); prints 282
  Serial.println(reading, 2); prints 100011010
  Serial.println(reading, 3); prints 101110
  Serial.println(reading, 4); prints 10122
  Serial.println(reading, 5); prints 2112
  Serial.println(reading, 6); prints 1150
  Serial.println(reading, 7); prints 552
  Serial.println(reading, 8); prints 432
  Serial.println(reading, 9); prints 343
  Serial.println(reading, 10); prints 282
  Serial.println(reading, 11); prints 237
  Serial.println(reading, 12); prints 1B6
  Serial.println(reading, 13); prints 189
  Serial.println(reading, 14); prints 162
  Serial.println(reading, 15); prints 13C
  Serial.println(reading, 16); prints 11A
  Serial.println(reading, 17); prints GA
  Serial.println(reading, 18); prints FC
  Serial.println(reading, 19); prints EG
  Serial.println(reading, 20); prints E2
*/


the 2nd arg to print indicates the # of decimal digits for a float/double

 0: 1
 1: 1.1
 2: 1.12
 3: 1.123
 4: 1.1235
 5: 1.12346
 6: 1.123457
 7: 1.1234568
 8: 1.12345683
 9: 1.123456835
 10: 1.1234568357
 11: 1.12345683574
 12: 1.123456835746
 13: 1.1234568357467
 14: 1.12345683574676
 15: 1.123456835746765
 16: 1.1234568357467651
 17: 1.12345683574676513
 18: 1.123456835746765136
 19: 1.1234568357467651367
 20: 1.12345683574676513671
const byte readingPin = A5;

void setup()
{
    Serial.begin(115200);

    double f = 1.1234567890;

    for (byte i = 0; i <= 20; i++)
    {
        Serial.print   (" ");
        Serial.print   (i);
        Serial.print   (": ");
        Serial.println (f, i);
    }
}

void loop()
{
}
int reading = analogRead(readingPin);  // read the value from a 10K pot

reading is declared as an int but you are trying to print it as if it were a float

The second parameter when you are printing a int is usually DEC, HEX etc each of which each have numeric values which is how the compiler is using your for loop value, hence the output that you are getting

To see what I mean, try printing the value of the HEX constant, for example

const byte readingPin = A5;

void setup()
{
  Serial.begin(115200);
  delay(300);

  float reading = (float)analogRead(readingPin);  // read the value from a 10K pot
  for (byte i = 0; i <= 20; i++)
  {
    Serial.print("Serial.println(reading, ");
    Serial.print(i);
    Serial.print("); prints ");
    Serial.println(reading, i);
  }
 }

void loop()
{
}

Thanks all, understood, I’ll change to a float.

And then what? the input value is still an int with no decimal part.

But it wouldn't be if you were to convert it to volts:

float reading = analogRead(readingPin)*5.0/1024.0;  // read the voltage from a 10K pot

Agreed, but it let me move confidently onto the next stage. I'm actually using several pots to determine values such as duration and volume. My problem was that I couldn't see how to get the rounded result I wanted. Printing examples with decimals was a requirement in fathoming that.

Here's a minimal version of code to get duration. Is there a neater way of doing the rounding?

const byte readingPin = A5;
//unsigned long duration;
float duration;

void setup()
{
  Serial.begin(115200);
  delay(300);

  // Get duration (0 to 60 mins)
  float reading = analogRead(readingPin);  // read the value from a 10K pot
  duration = 3600 * reading / 1023; // 0 to 1 hour, in s

  Serial.println(F("===================="));
  Serial.print(F("reading = "));
  Serial.println(reading);
  Serial.print(F("duration = "));
  Serial.print(duration);
  Serial.println(F( " s"));

  duration = duration / 60; // To get duration in minutes
  Serial.print(F("duration = "));
  Serial.print(duration);
  Serial.println(F( " mins")); // Prints 59, but it's almost 60
  // which is the rounded result I want printed.
  // By also changing duration to a float type, instead of unsigned
  // long, I get 59.94 mins printed.
  // I see no 'rounding' command?
  // So what is the recommended way to do so,
  // rather than using floor() or ceil() to round down or up?

  // Best I can manage is:
  if (ceil(duration) - duration < 0.5)
  {
    duration = ceil(duration);
  }
  else
  {
    duration = floor(duration);
  }
  Serial.print("duration rounded = ");
  Serial.println(duration);

}
  void loop()
  {
  }
  

Much neater, thanks!