# How can I determine the number of digits after the decimal point?

89 visualizzazioni (ultimi 30 giorni)
Iddo Weiner il 14 Mar 2015
Commentato: Iddo Weiner il 15 Mar 2015
Hi,
I am looking for a function that takes:
1. a matrix with different kinds of numbers - some are whole numbers (7), some have 1 number after the decimal point (12.3), some have 2 numbers (12.56) and so on...
2. some kind of instruction as to how many numbers after the decimal point I want
Example:
N=[7 12.3 4.78 5.505]
Let's say I want N to contain numbers with only 1 digit after the decimal point.
Is there a way to receive:
[N]=Some_Function(N,'1 digit')
N=[7.0 12.3 4.8 5.5] % meaning - round the numbers so they have only 1 number after the point
Or even (obviously not as good, but can provide a starting point):
N=[7 12.3 4.7 5.5] % meaning - just cut all numbers after the point with indexes greater than 1
Hope the question was clear,
Iddo
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Mark Hayworth il 14 Mar 2015
You can use the round() function to round a number to the specified number of places (though way out at the umpteenth place it might be non-zero, as per the FAQ.
out = round(yourNumber, 1);
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Iddo Weiner il 15 Mar 2015
Thank you very much - this was exactly what I needed Iddo

Accedi per commentare.

### Più risposte (1)

Image Analyst il 14 Mar 2015
The number of digits is the same. If you're using double class variables (the default), all use 64 bits of precision. How many digits are after the decimal place depends on how many digits are to the left of it. See the code example in the FAQ http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F which explains perfectly your situation.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Iddo Weiner il 14 Mar 2015
So, tell me if I understand you correctly, you're saying that what I want to do cannot be done, by default?

Accedi per commentare.

### Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by