473,804 Members | 4,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

float / rounding question

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

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

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

Of course the function above returns 53.600000000000 001.

How do I format it correctly?
Feb 25 '08 #1
13 2457
On Feb 25, 10:44 am, helen.m.fl...@g mail.com wrote:
Hi I'm very much a beginner with Python.
I want to write a function to convert celcius to fahrenheit like this
one:

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

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

Of course the function above returns 53.600000000000 001.

How do I format it correctly?
By the way, I tried this:

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

Any other suggestions?

Feb 25 '08 #2
sabatier wrote:
On Feb 25, 10:44 am, helen.m.fl...@g mail.com wrote:
>Hi I'm very much a beginner with Python.
I want to write a function to convert celcius to fahrenheit like this
one:

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

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

Of course the function above returns 53.600000000000 001.

How do I format it correctly?

By the way, I tried this:

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

Any other suggestions?
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.

Feb 25 '08 #3
25 February 2008 Monday 12:44:46 tarihinde he***********@g mail.com şunları yazmıştı:
Hi I'm very much a beginner with Python.
I want to write a function to convert celcius to fahrenheit like this
one:

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

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

Of course the function above returns 53.600000000000 001.

How do I format it correctly?
Use the round(number,di gits) function:

tf = round((9/5)*tc+32,1)
Feb 25 '08 #4
Necmettin Begiter <ne************ ***@gmail.comwr ote:
>25 February 2008 Monday 12:44:46 tarihinde he***********@g mail.com =C5=9Fun=
lar=C4=B1 yazm=C4=B1=C5=9 Ft=C4=B1:
>Of course the function above returns 53.600000000000 001.
=20
How do I format it correctly?

Use the round(number,di gits) function:

tf =3D round((9/5)*tc+32,1)
>>53.6
53.600000000000 001
>>round(53.6, 1)
53.600000000000 001

--
\S -- si***@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Feb 25 '08 #5
Mel
he***********@g mail.com wrote:
Hi I'm very much a beginner with Python.
I want to write a function to convert celcius to fahrenheit like this
one:

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

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

Of course the function above returns 53.600000000000 001.

How do I format it correctly?
print celcisuToFahren heit (12)

will do fine.
Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>def celciusToFahren heit(tc):
.... tf = (float(9)/5)*tc+32
.... return tf
....
>>celciusToFahr enheit (12)
53.600000000000 001
>>print celciusToFahren heit (12)
53.6
The straight value display from the interpreter pursues precision to
the bitter end, doing its formatting with the repr function. print
uses str formatting for a more expected result.

Mel.

Feb 25 '08 #6

<he***********@ gmail.comwrote in message
news:d4******** *************** ***********@z17 g2000hsg.google groups.com...
| Hi I'm very much a beginner with Python.
| I want to write a function to convert celcius to fahrenheit like this
| one:
|
| def celciusToFahren heit(tc):
| tf = (9/5)*tc+32
| return tf

Unless you are importing 'integer division' or using 3.0, that should be
9.0/5.0.

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

As written, running above on 2.x returns 44.

tjr

Feb 25 '08 #7
On Feb 26, 7:14 am, "Terry Reedy" <tjre...@udel.e duwrote:
<helen.m.fl...@ gmail.comwrote in message

news:d4******** *************** ***********@z17 g2000hsg.google groups.com...
| Hi I'm very much a beginner with Python.
| I want to write a function to convert celcius to fahrenheit like this
| one:
|
| def celciusToFahren heit(tc):
| tf = (9/5)*tc+32
| return tf

Unless you are importing 'integer division' or using 3.0, that should be
9.0/5.0.
Has the syntax changed? I thought it was:
from __future__ import division

The OP may wish to avoid the confusion and the pointless division by
using:
tf = 1.8 * tc + 32

>
| I want the answer correct to one decimal place, so
| celciusToFahren heit(12) would return 53.6.

As written, running above on 2.x returns 44.

tjr
Feb 25 '08 #8
On Mar 7, 5:12*pm, Piet van Oostrum <p...@cs.uu.nlw rote:
Python just uses the C library for printing, I presume, and the conversion
routines in the C library are rather simplistic. It is, however, possible
to do better, so that 53.6 -- although internally represented as something
that could be described as 53.600000000000 001 -- will actually be printed
as 53.6.
There are issues with doing this portably and reliably. See

http://bugs.python.org/issue1580

for a recent discussion.

Mark
Mar 8 '08 #9
On Fri, 07 Mar 2008 23:12:27 +0100, Piet van Oostrum wrote:
Sorry to come in so late in this discussion. Although it is correct to
say that many real numbers that have an exact decimal representation
cannot be exactly represented in binary, that is no excuse to print 53.6
as 53.600000000000 001. This is just lousy printing and the fact that
this kind of question comes up every week shows that it is confusing to
many people.
Good. That's a feature, not a bug.

Floats *are* confusing and unintuitive. Anyone with pretensions to be a
programmer should get over the illusion that floats are reals as soon as
possible, before they learn bad habits that have to be unlearned. If that
starts with them asking why they get 53.600000000000 001 instead of 53.6,
so be it. If they want to be insulated from the harsh reality, they can
do this:
>>print 53.6
53.6

--
Steven
Mar 8 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
16801
by: kdlittle88 | last post by:
Does anyone have any sample code on how to round off a float to x digits? say for example rounding 3.1415 to 3.1? Any help would be appreciated. I am learning C++ and have not found this info on the net (though it might be out there) tiny k
5
1358
by: lamthierry | last post by:
Are the following two similar? float a = 1.0f; float a = static_cast<float>(1.0); If they are, which one is preferrable? Thanks Thierry
14
15283
by: ziller | last post by:
Why is it that FLT_DIG (from <float.h>) is 6 while DBL_DIB is 15? Doing the math, the mantissa for floats is 24 bits = 2^24-1 max value = 16,777,215.0f. Anything 8-digit odd # greater than that will be rounded off. For doubles, the mantissa is 53 bits = 2^53-1 max value = 9,007,199,254,740,991.0l (that's an L). So 16 digit odd numbers greater than that will be rounded off. To get the actual precision we take log(base 10) of those...
6
7616
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards, Karthi
8
5463
by: abdul_n_khan | last post by:
Hello, I have a basic question related to datatypes. I am trying to read a value using Microsoft's ADO recordset from a field (lets call it 'Price') with datatype decimal(19,6) => 19 = Precision, 6 = Scale 1) When I read this field into float datatype. I get a value 1.9000, which is correct. But when I read its value in a double datatype I get 1.8999999761581.
12
13655
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically put the following code into your C# app: float testFloat2 = (int) (4.2f * (float)100); Console.Out.WriteLine("1: "+testFloat2); and the result will be 419
19
4696
by: morc | last post by:
hey, I have float values that look something like this when they are printed: 6.0E-4 7.0E-4 I don't want them to be like this I want them to be normalized with 4 decimal places.
116
36009
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
13
6194
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is ------------------ b += (float)(mode *val); On 32 bit(intel , vs 2003, C++), some watch variables are
2
5352
by: Mike | last post by:
I'm running DB2 v7 for z/OS. When I use SPUFI, SELECT CAST(6.0 AS FLOAT)/CAST(10.0 AS FLOAT) FROM SYSIBM.SYSDUMMY1 returns 0.6000000000000000E+00. When I use DSNTIAUL,DSNTEP2, or DSNALI (call attach facility), the same statement returns 0.59999999999999999E 00. The only reason I$B!G(Bve heard to explain this behavior is that float stores too much precision, but I$B!G(Bve used double-precision floating- point data types in SQL...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.