CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Count number of digits of a variable

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

Count number of digits of a variable
PostPosted: Thu Sep 22, 2016 11:53 am     Reply with quote

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: 350
Location: South Africa

View user's profile Send private message

PostPosted: Thu Sep 22, 2016 12:01 pm     Reply with quote

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: 291

View user's profile Send private message Visit poster's website

PostPosted: Sat Sep 24, 2016 1:02 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group