473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

does int() automatically call round()?

101 100+
I've tested it and it seems solid. I just want to make sure it always rounds correctly.

the original number is a float like:

Expand|Select|Wrap|Line Numbers
  1. var = 44.7
And I'm using int to change to an integer like so:

Expand|Select|Wrap|Line Numbers
  1. var = int(44.7)
  2. print var
output: 45

Will this always work? I just have to be accurate on my results.
Feb 5 '08 #1
6 1840
Laharl
849 Expert 512MB
If you want to be absolutely sure, call round() yourself each time.
Feb 6 '08 #2
A little experimentation shows that int() does not round.
>>> int(44.1)
44
>>> int(44.7)
44
>>> int(round(44.7))
45
on my machine. Please check your results again!

"Python in a Nutshell" (Martelli): "When x is a number, int(x) truncates toward 0, dropping any fractional part."

BTW, I recommend the Martelli book.
Feb 6 '08 #3
I don believe that int() calls round implicitly
>>> int(44.5)
44
>>> int(44.7)
44
these were my results

you can use the round method to round off without calling int()
>>> round(4.7)
5.0
>>> round(4.3)
4.0
if you want your results to be really acurate
Feb 7 '08 #4
zensunni
101 100+
Thanks for the reply. I'm pretty sure my tests actually rounded the number, but maybe my memory failed me. In any case, the inconsistancy is enough for me to make it explicit.

I'm glad I made sure.
Feb 8 '08 #5
Thanks for the reply. I'm pretty sure my tests actually rounded the number, but maybe my memory failed me. In any case, the inconsistancy is enough for me to make it explicit.

I'm glad I made sure.
Where is the inconsistancy???? It's pretty clear, the only thing that you have to do is read the docs...

Expand|Select|Wrap|Line Numbers
  1. int( [x[, radix]]) 
  2.  
  3. Convert a string or number to a plain integer. If the argument is a string, it must contain a possibly signed decimal number representable as a Python integer, possibly embedded in whitespace. The radix parameter gives the base for the conversion and may be any integer in the range [2, 36], or zero. If radix is zero, the proper radix is guessed based on the contents of string; the interpretation is the same as for integer literals. If radix is specified and x is not a string, TypeError is raised. Otherwise, the argument may be a plain or long integer or a floating point number. [b]Conversion of floating point numbers to integers truncates (towards zero)[/B]. If the argument is outside the integer range a long object will be returned instead. If no arguments are given, returns 0. 
Feb 8 '08 #6
zensunni
101 100+
that would probably be the inconsistancy of my "failed" memory and what people posted.

:)
Feb 10 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Simon | last post by:
Hi, I am having a little problem with my PHP - MySQl code, I have two tables (shown below) and I am trying populate a template page with data from both. <disclaimer>Now I would like to say my...
9
by: Vineet Jain | last post by:
int('2.1') does not work while int(float('2.1')) does. If int can covert a float object then there is no reason why a float string should not be converted too. When I do int('2.1') I get the...
74
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.