How to get the length of integer numbers ?


hello,

i newbie arduino, , last c++ coding experiences  many years ago...

i looking way position correctly integer number on lcd display. want place them right-aligned on display, need know length = number of characters of value.
example: looking function output of 3 input 100, 4 input -200, or 2 input 15.

my first approach convert number (int type) string, , use sizeof() command, found no appropriate  type conversion.

or there other feasible way placing values correctly right-aligned on lcd ?

regards,
tütenflieger

you can use itoa function convert string, , strlen function length of string.

or, simple nested if test:

code: [select]
int somevalue = 14156;
int vallen = 0;

if(somevalue > 9999)
  vallen = 5;
else if(somevalue > 999)
  vallen = 4;
else if(somevalue > 99)
  vallen = 3;
else if(somevalue > 9)
  vallen = 2;
else
  vallen = 1;


you'd need make changes if number negative (test absolute value , add 1 length).

if you're going pad spaces right align, might want @ using sprintf %nd format specifier, replacing n maximum number of digits value can have. way, padding done you, , strings same (known) length.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > How to get the length of integer numbers ?


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums