Matlab取整函数有: fix, floor, ceil, round.取整函数在编程时有很大用处。

一、取整函数

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为字符型变量。

matlab文本输出

两个函数:disp

fprintf

1、函数disp只带一个变量,他可以是自负矩阵或数值矩阵,要输出简单的文字信息,只需要用单引号将信息括起来:

>>disp(‘my favorite color is red’);

或者

>>yourname=input(‘enter your name’,’s’);

>>disp([‘your name is’,youname]);

例如

>> yourname = input('enter your name ','s');

enter your name panrq

>> disp(['your name is ',yourname]);

your name is panrq

选择带数值变量值的文本信息时,需要用函数num2str将数值变量的类型转换字符型

>> x=98;

>> outstring = ['x = ',num2str(x)];

>> disp(outstring);

x = 98

>>  disp(['x = ',num2str(x)]);

x = 98

disp函数只能带一个变量,表格中的各列需奥组合成一个矩阵,如下面的程序所示。

>> x=0:pi/5:pi;y=sin(x);

>> disp([x' y']);

0         0

0.6283    0.5878

1.2566    0.9511

1.8850    0.9511

2.5133    0.5878

3.1416    0.0000

Format命令

控制显示模式,直到下一个format出现前,这条format命令一直有效。

>> x=1.23456789;

>> format short;disp(pi);

3.1416

>> format long;disp(pi);

3.141592653589793

>> format short e;disp(pi);

3.1416e+000

>> format +;disp(pi);

+

>> format bank;disp(pi);

3.14

2、函数fprintf

fprintf(format);

fprintf(format,variables);

fprintf(fid,format,variables);

例如:

>> fprintf('i am concreten');

i am concrete

>> a=3;b='s';

>> fprintf('this is a %d and %s n',a,b);

this is a 3 and s

转载于:https://www.cnblogs.com/Simulation-Campus/p/8869925.html

matlab里面如何保留小数特定位数(转载)相关推荐

  1. matlab 小数点取前两位_matlab里面如何保留小数特定位数

    [转载]Matlab取整函数有: fix, floor, ceil, round.取整函数在编程时有很大用处. 一.取整函数 1.向零取整(截尾取整) fix-向零取整(Round towards z ...

  2. matlab 就近保留整数,matlab里面如何保留小数特定位,取整数及四舍五入

    matlab专区--------------matlab里 面如何保留小数特定位数 a=1.23456789 b=vpa(a,7) b = 1.234568 vpa(a,12) ans = 1.234 ...

  3. C#【保留小数点后位数】与【字符串数字格式化】

    保留小数点后位数 Double dValue = 95.12345;int iValue = 10000; string strValue = "95.12345"; string ...

  4. c#保留小数点后位数的方法

    Double dValue = 95.12345;int iValue = 10000;string strValue = "95.12345";string result = & ...

  5. python保留小数点后位数_Python保留指定位数的小数

    Python保留指定位数的小数 1 '%.2f' %f 方法(推荐) f = 1.23456 print('%.4f' % f) print('%.3f' % f) print('%.2f' % f) ...

  6. QT保留小数点后位数

    int k = 2; mm = 2.1622; QString xx = QString::number(mm,"f",k);  //保留k位小数 =====>>> ...

  7. ORACLE的保留小数或整数函数

    oracle的函数主要有这几种 1.ceil(n)取整(大),取比真值大的最小整数,注意正负数的取值: 如ceil(251.0001)=252,ceil(-251.0001)=-251,ceil(-2 ...

  8. js保留小数点后两位(js 小数点后两位)

    javascript中保留小数点后两位,第三位数字大于1时则进1 function fixN(t,l){ var n=t.toString().split('.')[1][l]; if(n>0& ...

  9. c语言double保留2位小数0,c语言 double类型保留小数问题

    c语言 double类型保留小数问题0 落寞的夕阳A2015.12.29浏览164次分享举报 用户输入希望保留小数的位数,1到16,怎么实现 int main(){ double a = 1.1234 ...

最新文章

  1. Android 自定义 —— View moveTo与 rMoveTo 的区别
  2. 海思3559A上编译LIVE555源码操作步骤
  3. 螃蟹不能吃的四个部位,爱吃螃蟹的一定要进来看看。
  4. python包管理器修改镜像地址
  5. 屌丝。。。配角。。。
  6. 磁盘上重复的贴图在内存中也会重复存在
  7. 【学术相关】科技论文写作:grammerly润色工具
  8. 怎样将html转换do,html代码格式化 Reddo的教程
  9. 庆祝.NET Core悄然崛起:免费送50本优秀技术书籍,请笑纳!!
  10. defineProperty AND defineProperties
  11. 设计模式--责任链模式
  12. Java或Web中解决所有路径问题
  13. [网络结构]DenseNet网络结构
  14. 人工智能白皮书 附下载地址
  15. cab补丁包离线安装命令
  16. 多声道在系统和软件里的设置与应用(普通声卡和dante声卡)
  17. 应届生程序员如何写好一份简历?
  18. “Tables资本局”专题中为啥木有新浪?
  19. 那些花儿那片笑声让我想起我的那些花儿在我生命每个角落静静为我开着我曾...
  20. 一文学会Maven私服的搭建

热门文章

  1. linux系统主要常见目录结构
  2. GIT SSH连接遇到到的问题
  3. [转]让你提升命令行效率的 Bash 快捷键
  4. Zend Studio 10代码格式化设置
  5. webbench网络压力测试源码
  6. Linq To select
  7. Vlan间路由实验(单臂路由实现法)
  8. jQuery对象与DOM对象的相互转化
  9. 【软件工程】第一次结对编程_作业
  10. ubuntu 16 下安装 Ubuntu Make