Truncating Decimal Places

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 件)

Walter Roberson
Walter Roberson 2012 年 2 月 23 日

0 投票

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.)

カテゴリ

質問済み:

2012 年 2 月 23 日

コメント済み:

2015 年 10 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by