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

### DSP Masters (view profile)

on 16 Nov 2011
Latest activity Commented on by Walter Roberson

on 19 Sep 2016

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

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

Jan Simon

### Jan Simon (view profile)

on 13 Feb 2016

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.

## Products

No products are associated with this question.

### Naz (view profile)

on 16 Nov 2011

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

Walter Roberson

### Walter Roberson (view profile)

on 16 Nov 2011

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

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

### Are Mjaavatten (view profile)

on 9 Feb 2016
Edited by Are Mjaavatten

### Are Mjaavatten (view profile)

on 9 Feb 2016

` mod(number,1)`

Walter Roberson

### Walter Roberson (view profile)

on 10 Feb 2016

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 (view profile)

on 13 Feb 2016

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 Simon

### Jan Simon (view profile)

on 13 Feb 2016

```abs(rem(-0.123, 1))   % =>  0.123
```

### Kh.Ehsanur Rahman (view profile)

on 13 Feb 2016

what if the number is -1.23.

### Walter Roberson (view profile)

on 14 Feb 2016

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

CS MATLAB

### CS MATLAB (view profile)

on 19 Sep 2016

What if the number is unknown and you want to compare decimal value with something..

Walter Roberson

### Walter Roberson (view profile)

on 19 Sep 2016

Comparing the fraction is risky

http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

If you want to compare to a certain number of decimal places, N, I recommend comparing round(number*10^N)

#### Join the 15-year community celebration.

Play games and win prizes!

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