필터 지우기
필터 지우기

How to seperate fractional and decimal part in a real number

조회 수: 390 (최근 30일)
DSP Masters
DSP Masters 2011년 11월 16일
댓글: Les Beckham 2023년 1월 25일
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
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일
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일
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일
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일
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]
Les Beckham
Les Beckham 2023년 1월 25일
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일
  댓글 수: 5
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일
what if the number is -1.23.

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by