MATLAB中输出科学计数法_Python基础—格式化输出(%⽤
法和format⽤法)
%⽤法
1、整数的输出
%o —— oct ⼋进制
%d —— dec ⼗进制
%x —— hex ⼗六进制
1 >>> print('%o' % 20)
2 24
3 >>> print('%d' % 20)
4 20
5 >>> print('%x' % 20)
6 14
2、浮点数输出
(1)格式化输出
%f ——保留⼩数点后⾯六位有效数字
%.3f,保留3位⼩数位
%e ——保留⼩数点后⾯六位有效数字,指数形式输出
%.3e,保留3位⼩数位,使⽤科学计数法
%g ——在保证六位有效数字的前提下,使⽤⼩数⽅式,否则使⽤科学计数法
%.3g,保留3位有效数字,使⽤⼩数或科学计数法
1 >>> print('%f' % 1.11) # 默认保留6位⼩数
2 1.110000
3 >>> print('%.1f' % 1.11) # 取1位⼩数
4 1.1
5 >>> print('%e' % 1.11) # 默认6位⼩数,⽤科学计数法
6 1.11
(2)内置round()
round(number[, ndigits])
参数:
number - 这是⼀个数字表达式。
ndigits - 表⽰从⼩数点到最后四舍五⼊的位数。默认值为0。
返回值
该⽅法返回x的⼩数点舍⼊为n位数后的值。
round()函数只有⼀个参数,不指定位数的时候,返回⼀个整数,⽽且是最靠近的整数,类似于四舍五⼊,当指定取舍的⼩数点位数的时
候,⼀般情况也是使⽤四舍五⼊的规则,但是碰到.5的情况时,如果要取舍的位数前的⼩数是奇数,则直接舍弃,如果是偶数则向上取舍。
注:“.5”这个是⼀个“坑”,且python2和python3出来的接⼝有时候是不⼀样的,尽量避免使⽤round()函数吧
1 >>> round(1.1125) # 四舍五⼊,不指定位数,取整
2 1
3 >>> round(1.1135,3) # 取3位⼩数,由于3为奇数,则向下“舍”
4 1.113
5 >>> round(1.1125,3) # 取3位⼩3、字符串输出
%s
%10s——右对齐,占位符10位
%-10s——左对齐,占位符10位
%.2s——截取2位字符串
%10.2s——10位占位符,截取两位字符串
1 >>> print('%s' % 'hello world') # 字符串输出
2 hello world
3 >>> print('%20s' % 'hello world') # 右对齐,取20位,不够则补位
4 hello world
5 >>> print('%-20s' % ' 4、 其他
(1)字符串格式代码
(2)常⽤转义字符
format⽤法
相对基本格式化输出采⽤‘%’的⽅法,format()功能更强⼤,该函数把字符串当成⼀个模板,通过传⼊的参数进⾏格式化,并且使⽤⼤括
号‘{}’作为特殊字符代替‘%’
位置匹配
(1)不带编号,即“{}”
(2)带数字编号,可调换顺序,即“{1}”、“{2}”
(3)带关键字,即“{a}”、“{tom}”
1 >>> print('{} {}'.format('hello','world')) # 不带字段
2 hello world
3 >>> print('{0} {1}'.format('hello','world')) # 带数字编号
4 hello world
5 >>> print('{0} {1} {0}'.forma >>> '{0}, {1}, {2}'.format('a', 'b', 'c')'a, b, c'>>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+版本⽀持'a, b, c'>>> '{2}, {1}, {0}'.format('a', 'b', 'c')'c, b, a'>>> '{2}, {1}, {0}'.format(*
>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')'Coordinates: 37.24N, -115.81W'>>> coord = {'latitude': '37.24N', 'l >>> c = 3-5j>>> ('The complex number {0} is formed from the real part {0.real} '... 'and the imaginary part {0.imag}.').format(c)'The complex number (3-5j) >>>>>> coord = (3, 5)>>> 'X: {0[0]}; Y: {0[1]}'.format(coord)'X: 3; Y: 5'>>> a = {'a': 'test_a', 'b': 'test_b'}>>> 'X: {0[a]}; Y: {0[b]}'.format(a)'X: test_a; Y: test_b
格式转换
'b' - ⼆进制。将数字以2为基数进⾏输出。
python格式化输出format'c' - 字符。在打印之前将整数转换成对应的Unicode字符串。
'd' - ⼗进制整数。将数字以10为基数进⾏输出。
'o' - ⼋进制。将数字以8为基数进⾏输出。
'x' - ⼗六进制。将数字以16为基数进⾏输出,9以上的位数⽤⼩写字母。
'e' - 幂符号。⽤科学计数法打印数字。⽤'e'表⽰幂。
'g' - ⼀般格式。将数值以fixed-point格式输出。当数值特别⼤的时候,⽤幂形式打印。
'n' - 数字。当值为整数时和'd'相同,值为浮点数时和'g'相同。不同的是它会根据区域设置插⼊数字分隔符。
'%' - 百分数。将数值乘以100然后以fixed-point('f')格式打印,值后⾯会有⼀个百分号。
1 >>> print('{0:b}'.format(3))
2 11
3 >>> print('{:c}'.format(20))
4
5 >>> print('{:d}'.format(20))
6 20
7 >>> print('{:o}'.format(20))
8 24
9 >>> print('{:x}'.form
进阶⽤法
进制转换
>>> # format also supports binary numbers>>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42)'int: 42; hex: 2a; oct: 52; bin: 101010'>>> # with 0x, 0o
左中右对齐及位数补全
(1)< (默认)左对齐、> 右对齐、^ 中间对齐、= (只⽤于数字)在⼩数点后进⾏补齐
(2)取位数“{:4s}”、"{:.2f}"等
左中右对齐及位数补齐
正负符号显⽰
>>> '{:+f}; {:+f}'.format(3.14, -3.14) # 总是显⽰符号'+3.140000; -3.140000'>>> '{: f}; {: f}'.format(3.14, -3.14) # 若是+数,则在前⾯留空格' 3.140000; -3.140000'>>>百分数%
>>> points = 19>>> total = 22>>> 'Correct answers: {:.2%}'.format(points/total)'Correct answers: 86.36%'
时间
>>> import datetime>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)'2010-07-04 12:15:58'逗号
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论