View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
Count number of digits of a variable |
Posted: Thu Sep 22, 2016 11:53 am |
|
|
Hi,
How can I count the number of digits of a variable...
A=[12875] how can I get 5 as the number of digits in A? |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Thu Sep 22, 2016 12:01 pm |
|
|
itoa() then len() of string.
Regards
Maybe I should have been more clear and checked the syntax of CCS before posting.
Code: |
char str[10];
int8 len;
itoa(A,10,str);
len = strlen(str);
|
len contains the number of decimals from A |
|
|
guy
Joined: 21 Oct 2005 Posts: 297
|
|
Posted: Sat Sep 24, 2016 1:02 pm |
|
|
much quicker:
is it>=10? if not, one digit.
else is it>=100? if not, two digits...
and to optimize ever further, start by comparing to the middle value. For 5-digit number, is it >=1000 ? This will statistically improve the timing. |
|
|
|