Good morning,
I would like to display these variables (a, b, c) as a 4-digit number, examples ( 1 = 0001), ( 12 = 0012), ( 123 = 0123), (1234 = 1234) .
Thank you for helping me.
int a = 0;
unsigned long b = 0;
float c = 0;
Good morning,
I would like to display these variables (a, b, c) as a 4-digit number, examples ( 1 = 0001), ( 12 = 0012), ( 123 = 0123), (1234 = 1234) .
Thank you for helping me.
int a = 0;
unsigned long b = 0;
float c = 0;
Sounds like a school assignment. You just missed the lecture where they talked about the / and % operators. I will give you one hint - what is the result of an integer divide of 1234 by 1000?
What you are needing is to display all numbers in 4 digit format with preceding 0s.
if # < 10, the print “000” then print the number.
else if # < 100 print “00” then the number
etc.
You could create a table of numbers and then index it with the variable:
char fourDigits[10000] =
{ "0000", "0001", "0002" ...
a = 1234;
Serial.println(fourDigits[a]);
![]()
c could be interesting, since it is a float. Do you want only the whole number, the whole number plus enough decimal places to make four digits, leading zeros with the decimal in a fixed location, etc.
aarg:
I will give you one hint - what is the result of an integer divide of 1234 by 1000?
1234 / 1000 = 1.234 ( a float )
You think I have to convert them all to float?
Thank you
Using sprintf formatting:
char buf [11] ; // large enough for biggest long int
sprintf (buf, "%04i", a) ; // leading zeroes, at least 4 chars.
Serial.print (buf) ;
mondoha:
1234 / 1000 = 1.234 ( a float )You think I have to convert them all to float?
Thank you
The intent is to do the math as integers.
This looks a lot like a school assignment, so most people are not going to post actual code showing how its done. You can find numerous examples of the various techniques if you search for how to print leading zeros.
larryd:
if # < 10, the print “000” then print the number.
else if # < 100 print “00” then the number
etc.
I have used this method because it has the advantage that you can leave empty spaces as I did with the variable c.
But if you want to write more variables and also with large numbers it becomes very difficult.
Thank you.
// Display a
if ( a < 10){ Serial.print("0000");}
else if ( a < 100){ Serial.print("000");}
else if ( a < 1000){ Serial.print("00");}
else if ( a < 10000){ Serial.print("0");}
Serial.print(a);
// Display b
if ( b < 10){ Serial.print("0000");}
else if ( b < 100){ Serial.print("000");}
else if ( b < 1000){ Serial.print("00");}
else if ( b < 10000){ Serial.print("0");}
Serial.print(b);
// Display c
if ( c < 10){ Serial.print(" ");}
else if ( c < 100){ Serial.print(" ");}
else if ( c < 1000){ Serial.print(" ");}
else if ( c < 10000){ Serial.print(" ");}
Serial.print(c);
MarkT:
Using sprintf formatting:char buf [11] ; // large enough for biggest long int
sprintf (buf, "%04i", a) ; // leading zeroes, at least 4 chars.
Serial.print (buf) ;
Thank you very much, it's a very good method and it works well.
char Numa [11] ;
char Numb [11];
char Numc [11];
sprintf (Numa, "%04i", a) ; // leading zeroes, at least 4 chars.
sprintf (Numb, "%04i", b) ; // leading zeroes, at least 4 chars.
sprintf (Numc, "%04i", c) ; // leading zeroes, at least 4 chars.
Serial.print(Numa) ;
Serial.print(" | ");
Serial.print(Numb) ;
Serial.print(" | ");
Serial.println(Numc) ;
Thank you
“ But if you want to write more variables and also with large numbers it becomes very difficult.”
You would create a function.
larryd:
You would create a function.
...and use iteration in an algorithm