How to Serial Analog Input as % of 5v?

I'm real new to the world of C based programming, so please, bear with me. I am trying to express TPS position on my car as a percentage. Scale is 0-5v, but really its closer to 0.4v-4.6v. I have tried everything I can think of but either my syntax is wrong or there is some lesson in variables I need to go over. Here is my code

#define TpsInput 1
int TpsVal;
void setup()
{
  Serial.begin(9600); 
}
  
  void loop()
{
  TpsVal = analogRead(TpsInput);
  Serial.println("TPS SIGNAL" );
  Serial.print(TpsVal);
  delay(1500);
}

I thought division by 1023 would get me the % I want, since using INT, but instead it turns up a weird value. I'm watching the 0-1023 on serial, so I know this much of the code is working. Can anyone help me out with what I am missing or suggest a tutorial I should go over? It seems like most things I find are using C? or Processing? besides the Arduino code, and since I know nothing about either of those, I am having a hard time learning through study of them. Thanks.

As a percentage of 5V you could write:

Serial.print((TpsVal/1023.0)*100); Serial.println("%");

--
The Rugged Motor Driver: two H-bridges, more power than an L298, fully protected

Thank you. I had tried "almost" that and I was not understanding the error messages it was returning.

Is Int OK for this? I was reading the example/tutorial pages while waiting for a reply, and I may have learned just enough to be dangerous to myself. :stuck_out_tongue:

While I understand why we use 1023, would it be better to use 1000, even though it loses a tiny fraction of accuracy? The reason I ask is because with division by 3 there is a chance of running into a repeating number. Or is that taken care of by the .0 you attached to the end of the 1023? Using this code I got an error one time that made everything return 0% until I reset the board, but I could have bumped something.

Since percentages are floating point numbers, all the values involved should be floats:

Serial.print(([glow](float)[/glow]TpsVal/1023.0)*100[glow].0[/glow]); Serial.println("%");