I want to store the digits after the decimal point of the numbers stored in an array. How do I do it?
13 views (last 30 days)
Show older comments
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
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.
Accepted Answer
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
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!