How to remove decimal??

47 visualizzazioni (ultimi 30 giorni)
baby
baby il 12 Mag 2012
Commentato: Walter Roberson circa 12 ore fa
Hello,,
i wanna know how to remove decimal??
Example a = 2.0000
how to remove zero beside point so it will be a = 2??
please help me
  1 Commento
Oleg Komarov
Oleg Komarov il 12 Mag 2012
Note that a is most likely not exactly 2, so the question is, do you want to "display" only the integer part or do you want to "drop" the decimal?

Accedi per commentare.

Risposta accettata

Oleg Komarov
Oleg Komarov il 12 Mag 2012
To make it clear:
a = 2.00000000001
a =
2.0000
Displaying the integer part ( ans is a char), but a retains the fraction:
sprintf('%.f',a)
ans =
2
Dropping the fraction ( ans is same class as a):
fix(a)
ans =
2

Più risposte (1)

Fabio
Fabio il 12 Mag 2012
As Oleg says if you want to 'display' the integer part just cast to String with num2Str() function: http://www.mathworks.it/help/techdoc/ref/num2str.html
If you want to drop the decimal part you can use round() function: http://www.mathworks.it/help/techdoc/ref/round.html
  2 Commenti
Luisa
Luisa circa 19 ore fa
Thank you. It worked for me. I needed to rewrite a = 6.7200 as 6.72.
Walter Roberson
Walter Roberson circa 12 ore fa
Note that when 6.7200 is displayed, chances are that you have "format short" in effect, and changing the format would change how it is displayed
a = 6.7200;
format short
a
a = 6.7200
format long g
a
a =
6.72
If you have format short in effect, then although you can round(), the format short puts the display back to 4 decimal places.
b = 6.72003;
format short
b
b = 6.7200
round(b,2)
ans = 6.7200
format long g
b
b =
6.72003
round(b,2)
ans =
6.72
If you have a specific output format needed, it is often best to use fprintf() or sprintf() or compose()

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by