Python3基础 str format 四舍六入五凑偶 保留一位小数

?

  • python : 3.7.0
  • OS : Ubuntu 18.04.1 LTS
  • IDE : PyCharm 2018.2.4
  • conda : 4.5.11
  • type setting : Markdown

?

code

[email protected]:~$ source activate py37
(py37) [email protected]:~$ ipython
Python 3.7.0 (default, Jun 28 2018, 13:15:42)
Type ‘copyright‘, ‘credits‘ or ‘license‘ for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type ‘?‘ for help.

In [1]: # 四舍

In [2]: "{0:.1f}".format(27.54)
Out[2]: ‘27.5‘

In [3]: "{0:.1f}".format(27.64)
Out[3]: ‘27.6‘

In [4]: 

In [4]: # 六入

In [5]: "{0:.1f}".format(27.56)
Out[5]: ‘27.6‘

In [6]: "{0:.1f}".format(27.66)
Out[6]: ‘27.7‘

In [7]: 

In [7]: # 五凑偶

In [8]: "{0:.1f}".format(27.55)
Out[8]: ‘27.6‘

In [9]: "{0:.1f}".format(27.65)
Out[9]: ‘27.6‘

In [10]: exit
(py37) [email protected]:~$ source deactivate py37
[email protected]:~$

?

resource

  • [文档] https://docs.python.org/3/
  • [规范] https://www.python.org/dev/peps/pep-0008/
  • [规范] https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_rules/
  • [源码] https://www.python.org/downloads/source/
  • [ PEP ] https://www.python.org/dev/peps/
  • [平台] https://www.cnblogs.com/

?



Python具有开源、跨平台、解释型、交互式等特性,值得学习。

Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。

代码的书写要遵守规范,这样有助于沟通和理解。

每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。

原文地址:https://www.cnblogs.com/xingchuxin/p/9696331.html

时间: 2024-08-03 11:07:09

Python3基础 str format 四舍六入五凑偶 保留一位小数的相关文章

Python3基础 format 四舍六入五凑偶 保留一位小数

镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.------------------------------------------ code: #四舍 str1="{0:.1f}".format(27.54) str2="{0:.1f}".format(27.64) print(str1) print(str2) #六入 str1="{0:.1f}".format(27

算法:四舍六入五成双 ,保留三位有效数字

/// <summary> /// 格式化 Decimal 数字 用于有效计算 /// </summary> /// <param name="ori"></param> /// <returns></returns> /// <remarks></remarks> public static decimal FormatDecimal(decimal ori) { if (ori == 0

在实现文本框只能输入数字和小数点的基础上实现了价格样式(保留两位小数)

我在测试充值接口的时候,输入金额保留到小数点后三位的时候程序就报错,后来就想到充值金额最多只能保留到小数点后两位,下面就是我用JQuery实现的详细代码,仅供参考,大神勿喷 1 $(function() { 2 //阻止数字键以外的按键输入 3 $("#zf_money").keydown(function(e) { 4 digitInput($(this),e); 5 }); 6 $("#zf_money").keyup(function() { 7 if($(t

java基础入门-float四舍五入保留两位小数注意的地方

float四舍五入保留两位小数注意的地方 最近在codewars做了一道非常简单的题目,但是它却花了我20来分钟,虽然最后解决了,但是中间发觉对于java的基础实在不行,回去要好好补一下 不说这么多,上题目:就是写一个方法,把英里每加仑换算成公里每升,保留两位数 一开始写的时候是没有问题,问题出现在保留两位数那里,我上一个测试代码 import java.util.regex.Pattern; public class Test { /* * 判断是否为整数 * * @param str 传入的

[Java基础] Java float保留两位小数或多位小数

方法1:用Math.round计算,这里返回的数字格式的. float price=89.89; int itemNum=3; float totalPrice=price*itemNum; float num=(float)(Math.round(totalPrice*100)/100);//如果要求精确4位就*10000然后/10000 方法2:用DecimalFormat 返回的是String格式的.该类对十进制进行全面的封装.像%号,千分位,小数精度.科学计算. float price=

Double 数据保留两位小数一:五舍六入

1 package com; 2 3 public class T2 { 4 5 public static void main(String[] args) { 6 7 System.out.println(calculateProfit(0.233)); 8 System.out.println(calculateProfit(0.235)); 9 System.out.println(calculateProfit(0.237)); 10 System.out.println(calcul

Python3基础 str translate 将指定字符转换成另一种特定字符

? python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 conda : 4.5.11 type setting : Markdown ? code [email protected]:~$ source activate py37 (py37) [email protected]:~$ ipython Python 3.7.0 (default, Jun 28 2018, 13:15:42) Type 'copyright'

Python3基础 str 循环输出list中每个单词及其长度

? python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 conda : 4.5.11 type setting : Markdown ? code """ @Author : 行初心 @Date : 18-9-23 @Blog : www.cnblogs.com/xingchuxin @GitHub : github.com/GratefulHeartCoder """ de

Python3基础 str *运算 重复拼接字符串

? python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 conda : 4.5.11 type setting : Markdown ? code [email protected]:~$ source activate xingchuxin (xingchuxin) [email protected]:~$ ipython Python 3.7.0 (default, Jun 28 2018, 13:15:42) Type