# How to seperate fractional and decimal part in a real number

783 views (last 30 days)
DSP Masters on 16 Nov 2011
Commented: Les Beckham on 25 Jan 2023
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..
Bart McCoy on 25 Jul 2018
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 on 14 Feb 2016
number = -1.23
integ = fix(number)
frac = mod(abs(number),1)
##### 2 CommentsShow 1 older commentHide 1 older comment
Walter Roberson on 19 Sep 2016
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)

Naz on 16 Nov 2011
number=1.23;
integ=floor(number);
fract=number-integ;
Walter Roberson on 16 Nov 2011
That fails on negative numbers. For negative numbers, you need fract=number-ceil(number)

Revant Adlakha on 24 Feb 2021
Edited: Revant Adlakha on 24 Feb 2021
sign(x)*(abs(x) - floor(abs(x)))
% Number -> x = -1.23
% Number -> x = 1.23
Setsuna Yuuki. on 15 Oct 2022
Thanks!

Resam Makvandi on 26 Dec 2012
Edited: Walter Roberson on 24 Feb 2021
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 CommentsShow 1 older commentHide 1 older comment
Les Beckham on 25 Jan 2023
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 on 9 Feb 2016
Edited: Are Mjaavatten on 9 Feb 2016
mod(number,1)
Jan on 13 Feb 2016
abs(rem(-0.123, 1)) % => 0.123

Kh.Ehsanur Rahman on 13 Feb 2016
what if the number is -1.23.