Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

How to seperate fractional and decimal part in a real number

Asked by DSP Masters

DSP Masters (view profile)

on 16 Nov 2011
Accepted Answer by Naz

Naz (view profile)

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..

2 Comments

Jan Simon

Jan Simon (view profile)

on 16 Nov 2011

Are you talking of numbers or strings? The quotes in '1.23' might be misleading.

Jerry Gregoire

Jerry Gregoire (view profile)

on 4 Oct 2012

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' !!

DSP Masters

DSP Masters (view profile)

Products

No products are associated with this question.

2 Answers

Answer by Naz

Naz (view profile)

on 16 Nov 2011
Accepted answer
number=1.23;
integ=floor(number);
fract=number-integ;

1 Comment

Walter Roberson

Walter Roberson (view profile)

on 16 Nov 2011

That fails on negative numbers. For negative numbers, you need fract=number-ceil(number)

Naz

Naz (view profile)

Answer by Resam Makvandi

Resam Makvandi (view profile)

on 26 Dec 2012

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.

0 Comments

Resam Makvandi

Resam Makvandi (view profile)

Contact us