This is not necessarily trivial to do reliably. In the first place, 31.456 is not representable in IEEE double exactly, so those trailing digits aren't really exactly all 0's anyway. E.g.,
>> num2strexact(31.456)
ans =
3.1455999999999999516830939683131873607635498046875e1
So, depending on how one prints the number out and how one sorts though all the trailing digits you can get a variety of answers. One approach might be to start with the exact string, as above, and then progressively round the last digit and re-read the result to see if you end up with the same IEEE double number. When it doesn't match, then you know you hit the smallest "significant" digit (assuming I understand what you mean by the term).
EDIT
OK, so here is a quick first cut at a function based on the idea above. Hasn't been extensively tested so caveat emptor. Seems to work for the few test cases I have thrown at it. Is not vectorized, although that could easily be done. (I hesitate to post this because I have this nagging suspicion that this could be done in a simpler way, but what the heck ...)
function n = sigdigits(x)
if( ~isnumeric(x) || ~isfinite(x) || isa(x,'uint64') )
error('Need any finite numeric type except uint64');
end
if( x == 0 )
n = 0;
return;
end
x = abs(x);
y = num2str(x,'%25.20e');
z = [' ' y];
n = find(z=='e') - 1;
e = n;
while( str2double(y) == str2double(z) )
zlast = z;
c = z(e);
if( c == '.' )
e = e - 1;
c = z(e);
end
z(e) = '0';
e = e - 1;
if( c >= '5' )
c = z(e);
if( c == '.' )
e = e - 1;
c = z(e);
end
while( true )
if( c == ' ' )
z(e) = '1';
break;
elseif( c < '9' )
z(e) = z(e) + 1;
break;
else
z(e) = '0';
e = e - 1;
c = z(e);
if( c == '.' )
e = e - 1;
c = z(e);
end
end
end
end
end
n = n - 1;
z = zlast(1:n);
while( z(n) == '0' )
n = n - 1;
end
n = n - 2;
end
Best Answer