python string.format()_Python string.format()百分比,不取整
In the example below I would like to format to 1 decimal place but python seems to like rounding up the number, is there a way to make it not round the number up?
>>> '{:.1%}'.format(0.9995)
'100.0%'
>>> '{:.2%}'.format(0.9995)
'99.95%'
解决方案
If you want to round down always (instead of rounding to the nearest precision), then do so, explicitly, with the math.floor() function:
from math import floor
def floored_percentage(val, digits):
val *= 10 ** (digits + 2)
return '{1:.{0}f}%'.format(digits, floor(val) / 10 ** digits)
print floored_percentage(0.995, 1)
Demo:
>>> from math import floor
>>> def floored_percentage(val, digits):
... val *= 10 ** (digits + 2)
... return '{1:.{0}f}%'.format(digits, floor(val) / 10 ** digits)
...
>>> floored_percentage(0.995, 1)
'99.5%'
>>> floored_percentage(0.995, 2)
'99.50%'
>>> floored_percentage(0.99987, 2)
'99.98%'
python string.format()_Python string.format()百分比,不取整相关推荐
- python中的除法、取整和求模_python中的除法,取整和求模
首先注明:如果没有特别说明,以下内容都是基于python 3.4的. 先说核心要点: 1. /是精确除法,//是向下取整除法,%是求模 2. %求模是基于向下取整除法规则的 3. 四舍五入取整roun ...
- python中的除法、取整和求模_python中的除法,取整和求模-Go语言中文社区
首先注明:如果没有特别说明,以下内容都是基于python 3.4的. 先说核心要点: 1. /是精确除法,//是向下取整除法,%是求模 2. %求模是基于向下取整除法规则的 3. 四舍五入取整roun ...
- python函数格式化_Python通过format函数格式化显示值
Python通过format函数格式化显示值,小数,小数点,转换成,科学,参数 Python通过format函数格式化显示值 易采站长站,站长之家为您整理了Python通过format函数格式化显示值 ...
- python中格式化_python的format格式化
使用方法: '{}bbccc'.format(aa) = aabbcc, 用来代替python2中的%,即替换. 1.通过位置来指定替换 In [2]: '{0},{1}'.format('a', ...
- python除以10取整_python ceiling divide 除法向上取整(或小数向上取整)的实例
python ceiling divide 除法向上取整(或小数向上取整)的实例 向上取整的方法: 方法1: items = 102 boxsize = 10 num_boxes = (items + ...
- python标准库math中用来实现上取整_Python之取整
在编程过程中数据处理是不可避免的,很多时候都需要根据需求把获取到的数据进行处理,取整则是最基本的数据处理.取整的方式则包括向下取整.四舍五入.向上取整等等.下面就来看看在Python中取整的几种方法吧 ...
- python标准库math中用来实现上取整_python中取整
向上取整 定义:大于或等于 x 的最大整数 math.ceil(x) 向上取整运算为Ceiling,用数学符号??表示 1 2 3 4 5 6>>> import math # py ...
- #python计算结果百位500向下取整,(0-499取000,500-999取500)
!/usr/bin/env python coding:utf-8 计算结果百位500向下取整,(0-499取000,500-999取500) import math calc_Amount = fl ...
- python怎么取百位_#python计算结果百位500向下取整,(0-499取000,500-999取500)
!/usr/bin/env python coding:utf-8 计算结果百位500向下取整,(0-499取000,500-999取500) import math calc_Amount = fl ...
- python浮点数整数、小数分离,整数取整
Python里面提供了很友好的浮点数整数取整或者是整数.小数部分分离的相关函数,之前这方面我使用的较为频繁地两个方法是ceil和floor,两个方法都是math模块提供的,分别完成的是对给定数字的向上 ...
最新文章
- [转载] 晓说——第3期:梦回青楼 爱与自由的温柔乡(上)
- r语言参数fig=c(),干货︱R语言绘图—基础图形参数整理
- Spring AOP概述及底层实现原理
- java stop和int,JAVA并发任务中止的isinterrupted,interupted()
- 嵌入式linux应用程序开发详解_【精品套餐】嵌入式linux应用驱动开发完全学习路线...
- 简单弄一个-个人主页
- 小世界网络模型代码 c 语言,新的小世界网络模型实现文本特征的提取方法与流程...
- 【PMP】Head First PMP 学习笔记 第十章 沟通管理
- Myeclipse快键键
- 在SIP对讲机中RTP/RTCP的实现
- count(1)、count(*) 与 count(列名) 的执行区别
- RK3399平台开发系列讲解(USB设备驱动)5.31、使用usb gadget configfs配置USB功能
- BZOJ5394: [Ynoi2016]炸脖龙(欧拉广义降幂)
- 织梦留言板模板 .php,DEDECMS 留言薄模块的使用方法
- PHP trait 特性
- 4.基于多目标粒子群算法冷热电联供综合能源系统运行优化
- 虚拟机学习(一)如何在虚拟机内安装系统
- python文件写入后,但文件还是空白
- Windows下VMware准备IOS开发环境
- 开关电源-TL431与光耦组成的电压反馈电路-TL431工作过程分析