How to get the same number of char as number of decimals using num2str?

Dear all,
I want to get the decimal part of a number.
I have written this small function:
function dec = get_decimals(input)
str = num2str(input) % convert to string
index = strfind(str,'.'); % locate the comma/point
dec = str2double(str(index+1:end)); % take the end
end
However if my input has too many decimals, the function will truncate my number:
>> dec = get_decimals(3.141592653)
str =
'3.1416'
decimals =
1416
How to solve it?
Thanks in advance.
Best,
louis

 Akzeptierte Antwort

input = pi
input = 3.1416
str = num2str(input,16)
str = '3.141592653589793'

1 Kommentar

Ok thanks a lot, below a function I suggest:
function dec = get_decimals(varargin)
% ---------------------------------------------
% ----- INFORMATIONS -----
% Function name : GET_DECIMALS
% Author : louis tomczyk
% Institution : Telecom Paris
% Email : louis.tomczyk.work@gmail.com
% Date : 2022-09-16
% Version : 1.0
%
% ----- MAIN IDEA -----
% Return the decimal part of a given input.
%
% ----- INPUTS -----
% VARARGIN{1} the number from which we want the decimals
% VARARGIN{2} the number of decimals wanted [OPTIONAL]
%
% ----- BIBLIOGRAPHY -----
% Functions :
% Author :
% Author contact :
% Date :
% Title of program :
% Code version :
% Type :
% Web Address :
% -----------------------
% Articles
% Author :
% Title :
% Jounal :
% Volume - N° :
% Date :
% DOI :
% ---------------------------------------------
input = varargin{1};
% conversion to string
if nargin == 1
str = num2str(input);
else
str = num2str(input,varargin{2});
end
% locating the decimals
index = strfind(str,'.');
% getting the decimals
dec = str2double(str(index+1:end));
% if no decimals
if isnan(dec)
dec = [];
end
end
Best,

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

James Tursa
James Tursa am 16 Sep. 2022
You might look into using the fix( ) function to isolate the fractional part of your number and then work with that directly.

7 Kommentare

Dear James Tursa, thanks for your suggestion but it seems to me less easy to make something with.
James Tursa
James Tursa am 16 Sep. 2022
Bearbeitet: James Tursa am 16 Sep. 2022
It is unclear to me whether you want truncated digits as your original post, or rounded digits as your later post. Regardless, here is what I meant by using the fix( ) function to isolate the fractional part:
f = x - fix(x)
Then you could work with f directly using num2str( ) or some other method.
OK, let me explain more in detail.
Truncated or rounded doesn't matter to me at all. I just wanted to know if an input is an integer or not.
I thought I could do it with the function but it doesn't seem to be that straightforward. Indeed with the following examples:
isinteger(4)
ans = logical
0
isinteger(int8(4))
ans = logical
1
isinteger(int8(4.1))
ans = logical
1
So another way to do using your solution would be something like:
dec = 4.1-fix(4.1)
dec = 0.1000
but checking is it's null or not doesn't work as:
dec == 0
ans = logical
0
dec == 0.1
ans = logical
0
So I don't know how to get further following this way...
@Louis Tomczyk is the purpose is to test a number is an integer (not the class is subtypes of integer) why not simply use modulo (try to avoid to convert to string that is only convenient for human but not so for computer)
isint = @(x) mod(double(x),1) == 0;
% Few examples
isint(3)
ans = logical
1
isint(-10)
ans = logical
1
isint(pi)
ans = logical
0
isint(1+eps)
ans = logical
0
Louis Tomczyk
Louis Tomczyk am 16 Sep. 2022
Bearbeitet: Louis Tomczyk am 16 Sep. 2022
It is definitely the best answer to my (true) problem.
I would have accepted it but my initial question was about ...
Thanks a lot for your time.
Best,
louis
James Tursa
James Tursa am 16 Sep. 2022
Bearbeitet: James Tursa am 16 Sep. 2022
@Louis Tomczyk As you have just learned, it is best to post your real problem up front so as to avoid answers that don't really help you. In addition to Bruno's latest comment, here are other methods for determining if a finite number is an integer value:
x==fix(x)
x==floor(x)
x==round(x)
I know it, but usually I like to try things my own way, not getting directly answer which will surely be much more efficient.
I like to see the reasoning process suggested by others to get the best of this sharing experience.
Thanks again anyway to both of you.
Best,
louis

Melden Sie sich an, um zu kommentieren.

Produkte

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by