I want to store the digits after the decimal point of the numbers stored in an array. How do I do it?

12 views (last 30 days)
For eg. If one of my number is 1.23456789 then I want '234' out of it. I want only the 3 digits after the decimal point. Can you help me?
  2 Comments
Stephen23
Stephen23 on 7 Oct 2014
Edited: Stephen23 on 7 Oct 2014
It is not completely clear: do you want a character array (e.g. '234' ) or a numeric vector of the digits (e.g. [2,3,4] ) as the output?
Image Analyst
Image Analyst on 7 Oct 2014
Edited: Image Analyst on 7 Oct 2014
He said the character string, but my guess is that he really wants a single 3 digit integer (or double) just like what dpb's code give.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 7 Oct 2014
Edited: Stephen23 on 7 Oct 2014
To get the digits in a scalar numeric:
>> x = 1.23456789;
>> N = 4;
>> n = fix(rem(x,1)*10^N)
n = 2345
then to convert this to a string:
>> s = sprintf('%.0f',n)
s = '2345'
and finally to get the digits in a numeric vector:
>> v = s-'0'
v = [2,3,4,5]
  2 Comments
Stephen23
Stephen23 on 4 Jan 2015
Edited: Stephen23 on 4 Jan 2015
An alternative way to generate a string and numeric vector of the decimal digits:
>> N = 4; % how many digits
>> A = 1.23456789;
>> B = sprintf('%.*f',N,mod(A,1)-mod(A,10^-N));
>> B(3:end)
ans = '2345'
>> B(3:end)-'0'
ans = [2,3,4,5]

Sign in to comment.

More Answers (1)

dpb
dpb on 7 Oct 2014
fix(rem(x,fix(x))*1000)
  3 Comments

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!