一、取整函數
1.向零取整(截尾取整)
fix-向零取整(Round towards zero);
>> fix(3.6)
ans = 3
2.向負無窮取整(不超過x 的最大整數-高斯取整)
floor-向負無窮取整(Round towards minus infinity);
>> floor(-3.6)
ans = -4
3.向正無窮取整(大於x 的最小整數)
ceil-向正無窮取整(Round towards plus infinity);
>> ceil(-3.6)
ans = -3
4.向最近整數取整,四舍五入(四舍五入取整)
round-向最近整數取整,四舍五入(Round towards nearest integer);
>> round(3.5)
ans = 4
二、在小數點后某一位四舍五入,即保留幾位小數,也經常用到。
1.數值型 roundn—任意位位置四舍五入
>>a=123.4567890;
>>a=roundn(a,-4)
a = 123.4568
其中roundn函數功能如下:
y = ROUNDN(x) rounds the input data x to the nearest hundredth. %不指定n,精確到百分位 y = ROUNDN(x,n) rounds the input data x at the specified power %精確到小數點后指定位數n
2.符號型
digits(4)
vpa(....)
必須說明:vpa命令不能識別整數與小數,只算總位數,因此對它來說小數整數無論哪個都占一位,例如對9.3154保留兩位小數時就得寫成:
>>a=9.3154;
>>digits(3)
>>b=vpa(a)
b= 9.32
其中b為符號型變量;
3.字符型
>>a=12.34567;
>>b = sprintf('%8.2f',a)
b = 12.35 其中b為字符型變量。