Truncating Decimal Places

440 vues (au cours des 30 derniers jours)
Tarik Razak
Tarik Razak le 23 Fév 2012
Commenté : suman pani le 4 Oct 2015
Hi I am trying to figure out how to truncate the decimal places.
Following code
sprintf('%g',round(pi*100)/100)
gives me answer of 3.14
I want to either get 3 as answer or 4 either one is fine.
Any ideas how to do it right.
Thanks
  1 commentaire
suman pani
suman pani le 4 Oct 2015
use 'ceil' for 4 and 'floor' for 3. that is
ceil(pi*100/100)=4
floor(pi*100/100)=3

Connectez-vous pour commenter.

Réponses (2)

Honglei Chen
Honglei Chen le 23 Fév 2012

Walter Roberson
Walter Roberson le 23 Fév 2012
round(), fix(), floor(), ceil() are all important in different contexts. (Some of them will look the same at first, but if you study the behavior for negative numbers you will see differences.)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by