# grabbing number after decimal

214 Ansichten (letzte 30 Tage)
Neesha am 20 Aug. 2014
Kommentiert: Elena am 22 Feb. 2022
Hi I have a dataset which has column 'specialNumber'. It has data like '1234.5, 9087.3...' and so on, I want to extract number after decimal. so for 1234.5 = 5 for example. How do i do that
##### 1 KommentarKeine anzeigenKeine ausblenden
Bart McCoy am 25 Jul. 2018
The "fix" function is useful here, since it rounds toward 0. This is important when extracting the fractional part of a NEGATIVE number:
If value = pi, then "value - fix(value)" = 0.141592653589793
If value = -pi, then "value - fix(value)" = -0.141592653589793

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Star Strider am 20 Aug. 2014
If you know you will only have one number after the decimal, just use the rem function:
x = 1234.5;
d = 10*rem(x,1);
produces:
d =
5
##### 1 KommentarKeine anzeigenKeine ausblenden
Elena am 22 Feb. 2022
how would you do this to for a number like 0.05120? I would only like to get the '05' value from this. (last two numbers after decimal)

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (5)

David Sanchez am 20 Aug. 2014
N = 1234.5;
R = 10*(N-floor(N))
R =
5
##### 3 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Patrik Ek am 20 Aug. 2014
Bearbeitet: Patrik Ek am 20 Aug. 2014
@Hikaru It is true that R becomes 50 when there is only 1 decimal digit in the number. This is not a bug though, since the second digit indeed would be 0 if N is exactly 1234.5. If the accuracy of N is one digit (or if you only want the first decimal digit), it is still not a problem. Then you explicitely need to state that you do only want one digit and not 2 by setting nDigits=1. This is not an unreasonable approach, since this decision should really be the user's.
And about using round instead of floor I do not buy it. You solve a problem that is unlikely to happen, by creating a problem that will occur in 50% of all the cases. Assume that I would use round in my case; This would give the result R = 54, since round(53.7) is 54. This could possibly be a common case. However, since you do the operation N-floor(N), you are unlikely to get floating point errors. Focus on the real problem instead of obscure errors that will probably not occur anyway. An alternative could be you uploading a solution that is safe from floating point errors. To use round is not really a solution to the problem. It is just a workaround and in this case a workaround that does not work as intended.

Melden Sie sich an, um zu kommentieren.

Image Analyst am 20 Aug. 2014
neesha, what if there are 3 or 4 numbers after the decimal point? Do you want only the first one, or all of them? What if you did
fractions = t.specialNumber - floor(t.specialNumber);
where t is the table and specialNumber is the column in the table. So for example 73.8234 would give 0.8234 as a result. Would that work for you? Or do you only want the 8 as 8 and not as 0.8 or 0.8234?
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Hikaru am 20 Aug. 2014
Bearbeitet: Hikaru am 20 Aug. 2014
One way to do this
specialNumber = [1234.5; 9087.3];
d = abs(specialNumber - fix(specialNumber));
answer = d*10; %assuming that your data only has one decimal place
Note: This is not really the best solution since subtracting floating points can introduce error.
##### 1 KommentarKeine anzeigenKeine ausblenden
Patrik Ek am 20 Aug. 2014
Well I guess that you are quite safe from floating point errors here, since you you use the same variable. However, I think that a completely stable solution would cost more than you would gain. Floating point errors in operations like this is uncommon in matlab.

Melden Sie sich an, um zu kommentieren.

Patrik Ek am 20 Aug. 2014
Since there have been a discussion about floating point errors here (which IMO seems to be overkill to care about for this operation) I will provide an integer casting solution. This should be free from floating point issues since no addition or subtraction is done with floating points.
N = 1234.537;
nDigits = 2;
R = cast(floor(10^nDigits*N),'int32') - cast(10^nDigits*floor(N),'int32');
R = cast(R,'double') % If double is important then cast it back
The floor in the first term is there because of matlabs solution of casting, which rounds the double to closest int.
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Neesha am 20 Aug. 2014
Hello everybody, thanks a lot for fantatic solutions. While all of your solution seem to work, since i have only one decimal after the number , following works out just fine
x = 1234.5; d = 10*rem(x,1); produces:
d = 5
thanks a bunch again
##### 1 KommentarKeine anzeigenKeine ausblenden
Star Strider am 20 Aug. 2014
My pleasure!

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on Logical in Help Center and File Exchange

### Tags

Noch keine Tags eingegeben.

### Community Treasure Hunt

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

Start Hunting!