Asked by joseph Frank
on 3 Jul 2011

Hi,

how can I compute the number of digits A=[12875] how can I get 5 as the number of digits in A?

*No products are associated with this question.*

Answer by bym
on 3 Jul 2011

Accepted answer

One way:

numel(num2str(A))

Paulo Silva
on 3 Jul 2011

Answer by Oleg Komarov
on 3 Jul 2011

To count integer part

ceil(log10(abs(A)))

**Edit**

floor(log10(abs(A)+1)) + 1

Walter Roberson
on 15 Jul 2011

Fails on 0, Jan.

Over integral values:

ceil(log10(max(1,abs(A)+1)))

Over real numbers,

ceil(log10(max(1,abs(A)*(1+eps))))

I think.

Answer by Jaymin
on 13 Dec 2012

Long, but it gets the job done.

numel(num2str(A))-numel(strfind(num2str(A),'-'))-numel(strfind(num2str(A),'.'))

Answer by Turner
on 16 Aug 2013

Edited by Turner
on 19 Aug 2013

Will do the trick for all nonzero integers:

fix(abs(log10(abs(A))))+1

For a 10,000 iteration benchmark with some above answers:

Jaymin= 1.423717 seconds; Stephanie= 0.476135 seconds; Mine= 0.000878 seconds

If you don't expect 0s to appear, this is the fastest and most accurate method. Only works for decimals that satisfy -1<A<1.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments