漂浮/舍入问题 [英] float / rounding question

查看:74
本文介绍了漂浮/舍入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我是Python的初学者。

我想编写一个将celcius转换为华氏温度的函数

one:


def celciusToFahrenheit(tc):

tf =(9/5)* tc + 32

返回tf


我希望答案正确到一个小数位,所以

celciusToFahrenheit(12)将返回53.6。


当然上面的函数返回53.600000000000001。


如何正确格式化?

Hi I''m very much a beginner with Python.
I want to write a function to convert celcius to fahrenheit like this
one:

def celciusToFahrenheit(tc):
tf = (9/5)*tc+32
return tf

I want the answer correct to one decimal place, so
celciusToFahrenheit(12) would return 53.6.

Of course the function above returns 53.600000000000001.

How do I format it correctly?

推荐答案

2月25日上午10点44分,helen.m.fl ... @ gmail.com写道:
On Feb 25, 10:44 am, helen.m.fl...@gmail.com wrote:

你好我是Python的初学者。

我想编写一个将celcius转换为华氏温度的函数

one:


def celciusToFahrenheit(tc):

tf =(9/5)* tc + 32

返回tf


我希望答案正确到小数点后一位,所以

celciusToFahrenheit(12)会返回53.6。


当然上面的函数返回53.600000000000001。


如何正确格式化?
Hi I''m very much a beginner with Python.
I want to write a function to convert celcius to fahrenheit like this
one:

def celciusToFahrenheit(tc):
tf = (9/5)*tc+32
return tf

I want the answer correct to one decimal place, so
celciusToFahrenheit(12) would return 53.6.

Of course the function above returns 53.600000000000001.

How do I format it correctly?



顺便说一句,我试过这个:


返回''%2.1f''%tf但返回一个字符串而不是数字。


还有其他任何建议吗?

By the way, I tried this:

return ''%2.1f'' % tf but that returns a string instead of a number.

Any other suggestions?


sabatier写道:
sabatier wrote:

2月25日上午10点44分,helen.m.fl ... @ gmail.com写道:
On Feb 25, 10:44 am, helen.m.fl...@gmail.com wrote:

> ;你好我是Python的初学者。
我想编写一个函数将celcius转换为华氏温度这样的一个:

def celciusToFahrenheit(tc):
tf =(9/5)* tc + 32
返回tf

我希望答案正确到小数点后一位,所以
celciusToFahrenheit(12)会返回53.6。

当然上面的函数返回53.600000000000001。

如何正确格式化?
>Hi I''m very much a beginner with Python.
I want to write a function to convert celcius to fahrenheit like this
one:

def celciusToFahrenheit(tc):
tf = (9/5)*tc+32
return tf

I want the answer correct to one decimal place, so
celciusToFahrenheit(12) would return 53.6.

Of course the function above returns 53.600000000000001.

How do I format it correctly?



顺便说一句,我试过这个:


返回''%2.1f''%tf但返回一个字符串而不是数字。


还有其他建议吗?


By the way, I tried this:

return ''%2.1f'' % tf but that returns a string instead of a number.

Any other suggestions?



但是你要求上面的格式字符串上有一个字符串。


而且格式化在其他上下文中没有意义因为一个数字是一个

数字,53.600000和53.6是相同的数字(除了精度)。


你关心的是数字在二进制中的表示方式。当

显示值时,请使用上面显示的格式字符串,所有这些都将

工作。

But you are asking for a string on your format string above.

And also formatting make no sense in other context since a number is a
number and 53.600000 and 53.6 are the same number (besides precision).

You are concerned with how numbers are represented in binary. When
displaying the value use the format string you shown above and all will
work.


2008年2月25日星期一12:44:46 tarihinde 他*********** @ gmail .com ?? unlar?±yazm?±?? t?±:
25 February 2008 Monday 12:44:46 tarihinde he***********@gmail.com ??unlar?± yazm?±??t?±:

你好我是Python的初学者。 />
我想编写一个函数将celcius转换为华氏温度这样的

一:


def celciusToFahrenheit(tc):

tf =(9/5)* tc + 32

返回tf


我希望答案正确到小数点后一位,所以

celciusToFahrenheit(12)将返回53.6。


当然上面的函数返回53.600000000000001。


我如何格式化正确吗?
Hi I''m very much a beginner with Python.
I want to write a function to convert celcius to fahrenheit like this
one:

def celciusToFahrenheit(tc):
tf = (9/5)*tc+32
return tf

I want the answer correct to one decimal place, so
celciusToFahrenheit(12) would return 53.6.

Of course the function above returns 53.600000000000001.

How do I format it correctly?



使用圆(数字,数字)函数:


tf = round((9/5)* tc + 32,1)

Use the round(number,digits) function:

tf = round((9/5)*tc+32,1)


这篇关于漂浮/舍入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆