How to seperate fractional and decimal part in a real number

Hi, Please help me in seperating fractional and decimal part in a real number. For example: If the value is '1.23', I need to seperate decimal part '1' and 'fractional part '0.23'.
Thanks and regards, soumya..

5 件のコメント

Jan
Jan 2011 年 11 月 16 日
Are you talking of numbers or strings? The quotes in '1.23' might be misleading.
Jerry Gregoire
Jerry Gregoire 2012 年 10 月 4 日
Jan Its my pet peeve when a poster poses a question and it is responded to with another unnecessary question. Yes, in Matlab syntax, '0.23' indicates a string, but it is really obvious that he meant 0.23. I guess my wish to responders is simply, 'Just answer the question already' !!
Jan
Jan 2016 年 2 月 13 日
Some years later: @Jerry: Many questions in this forum are based on the inaccurate knowledge about the classes of variables. I tend to ask for a clarification instead of speculating of what seems obvious.
Jeremy Wood
Jeremy Wood 2017 年 7 月 5 日
Try using the floor operator to get the greatest integer below your number then subtract out your integer. For example 1.5 - floor(1.5) 0.5. It's trickier with negative numbers though so try using the absolute value of the number then when you get your fractional part multiply it by -1 so for -1.5 you would do -1*(1.5 - floor(1.5))
Bart McCoy
Bart McCoy 2018 年 7 月 25 日
EXTRACTING THE INTEGER PART
Extracting the integer part can be the most tricky part. MATLAB's "fix" function rounds toward zero, which is useful because it extracts the integer part of BOTH positive and negative numbers. It returns doubles and also works on NxM arrays.
By contrast, the "ceil" function always rounds upward, to the next integer in the POSITIVE direction; "floor" always rounds down, to the next integer in the NEGATIVE direction. Use whatever makes sense, but note:
INTEGER EXTRACTION: fix(pi) = 3; fix(-pi) = -3;
ROUNDING UP: ceil(pi) = 4; ceil(-pi) = -3;
ROUNDING DOWN: floor(pi) = 3; floor(-pi)= -4;
EXTRACTING THE FRACTIONAL PART:
fractional_part = value - fix(value);

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2016 年 2 月 14 日

4 投票

number = -1.23
integ = fix(number)
frac = mod(abs(number),1)

2 件のコメント

CS MATLAB
CS MATLAB 2016 年 9 月 19 日
What if the number is unknown and you want to compare decimal value with something..
Walter Roberson
Walter Roberson 2016 年 9 月 19 日
Comparing the fraction is risky
If you want to compare to a certain number of decimal places, N, I recommend comparing round(number*10^N)

サインインしてコメントする。

その他の回答 (5 件)

Naz
Naz 2011 年 11 月 16 日

9 投票

number=1.23;
integ=floor(number);
fract=number-integ;

1 件のコメント

Walter Roberson
Walter Roberson 2011 年 11 月 16 日
That fails on negative numbers. For negative numbers, you need fract=number-ceil(number)

サインインしてコメントする。

Revant Adlakha
Revant Adlakha 2021 年 2 月 24 日
編集済み: Revant Adlakha 2021 年 2 月 24 日

1 投票

How about this?
sign(x)*(abs(x) - floor(abs(x)))
% Number -> x = -1.23
% Answer -> -0.23
% Number -> x = 1.23
% Answer -> 0.23
Resam Makvandi
Resam Makvandi 2012 年 12 月 26 日
編集済み: Walter Roberson 2021 年 2 月 24 日

0 投票

i think the better way is to use:
number = 1.23;
integ = fix(number);
fract = abs(number - integ);
it works for both negative and positive values.

2 件のコメント

KOMAL VERMA
KOMAL VERMA 2023 年 1 月 25 日
what if there is array
like x=[0.2, 1.2 1.0]
Did you try it?
x = [0.2, 1.2 1.0]
x = 1×3
0.2000 1.2000 1.0000
integ = fix(x)
integ = 1×3
0 1 1
fract = abs(x - integ)
fract = 1×3
0.2000 0.2000 0

サインインしてコメントする。

Are Mjaavatten
Are Mjaavatten 2016 年 2 月 9 日
編集済み: Are Mjaavatten 2016 年 2 月 9 日

0 投票

mod(number,1)

5 件のコメント

Walter Roberson
Walter Roberson 2016 年 2 月 9 日
>> mod(-0.123,1)
ans =
0.877
However, 0.877 is neither the whole number nor the fraction of -0.123
Are Mjaavatten
Are Mjaavatten 2016 年 2 月 10 日
Walter is right of course. To work for both positive and negative numbers my solution must be mofified to
mod(abs(number),1)*sign(number)
or just
mod(abs(number),1)
depending on you definition of the fraction part. I prefer these to the accepted answer because it does not require intermediate variables, but this is a matter of taste.
Walter Roberson
Walter Roberson 2016 年 2 月 10 日
The accepted answer by Naz does not use any intermediate variables. The task is to return each of the parts. Naz's solution happens to calculate one part and use it to calculate the other as well, but that does not make either one an intermediate variable.
Are Mjaavatten
Are Mjaavatten 2016 年 2 月 13 日
Point taken. I should be old enough to have learned to read the problem definition. Still, I think it is nice to have a single command for the fractional part.
Jan
Jan 2016 年 2 月 13 日
What about rem instead of mod?
abs(rem(-0.123, 1)) % => 0.123

サインインしてコメントする。

Kh.Ehsanur Rahman
Kh.Ehsanur Rahman 2016 年 2 月 13 日

0 投票

what if the number is -1.23.

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by