Truncating Decimal Places

조회 수: 441 (최근 30일)
Tarik Razak
Tarik Razak 2012년 2월 23일
댓글: suman pani 2015년 10월 4일
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
suman pani
suman pani 2015년 10월 4일
use 'ceil' for 4 and 'floor' for 3. that is
ceil(pi*100/100)=4
floor(pi*100/100)=3

댓글을 달려면 로그인하십시오.

답변 (2개)

Honglei Chen
Honglei Chen 2012년 2월 23일

Walter Roberson
Walter Roberson 2012년 2월 23일
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.)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by