Python中文
首页
教程
问答
标签
搜索
登录
注册
Python中实现任意精度舍入的有效方法
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>什么是Python解决以下问题的方法?在</p> <p>我在读一个分辨率为0.5的温度传感器。我需要写它(它有一个可编程的恒温器输出),分辨率也是0.5。在</p> <p>因此,我编写了这个函数(Python2.7),将作为输入的浮点值四舍五入到最接近的位置。5:</p> <pre><code>def point5res(number): decimals = number - int(number) roundnum = round(number, 0) return roundnum + .5 if .25 <= decimals < .75 else roundnum print point5res (6.123) print point5res(6.25) print point5res(6.8) </code></pre> <p>工作正常,分别输出6.0、6.5和7.0。这正是我想要的。在</p> <p>我对Python比较陌生。线</p> ^{pr2}$ <p>让我对it实现者垂涎欲滴。但它是Python吗?在</p> <p>编辑:自从发帖以来,我对<a href="http://blog.startifact.com/posts/older/what-is-pythonic.html" rel="nofollow">what is and isn't 'Pythonic'</a>有了更多的了解。我的代码不是。Cmd的密码,下面是。谢谢您!在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<pre><code>return 0.5 * divmod (number, 0.5) [0] if number >= 0 else -0.5 divmod (number, -0.5) [0] </code></pre> <p>不过,这看起来更好:</p> ^{pr2}$
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
Python中是否有类似Clojure的线程宏?
5 回答
Python中是否有类似JPanel的组件?
7 回答
Python中是否有类似MATLAB的vpasolve的等价解算器
1 回答
python中是否有类似NLTK的东西不需要安装?
7 回答
python中是否有类似osgi在java中试图解决的需求?
10 回答
python中是否有类似PAM的模块?
8 回答
Python中是否有类似Perl的Data::Dumper的工具用来检查数据结构?
3 回答
python中是否有类似robocy的函数,带有重试选项?
7 回答
Python中是否有类似Rstudio的帮助部分?
5 回答
python中是否有类似ruby的索引方法?
4 回答
python中是否有类似于“perlpe”选项的内容?
7 回答
Python中是否有类似于C++ STL映射的结构?
6 回答
python中是否有类似于C中预处理器行为的工具?
7 回答
Python中是否有类似于Harmony的let关键字的内容?
1 回答
python中是否有类似于matlab中fzero的函数?
6 回答
python中是否有类似于MATLAB中peaks(N)的函数/实现?
2 回答
Python中是否有类似于Matlab的deconvblind的函数?
5 回答
Python中是否有类似于Perl中“想要”的东西
7 回答
Python中是否有类似于Perl正则表达式中的"local"变量?
6 回答
python中是否有类似于php的$$变量的语法
10 回答