41

I have made a program that divides numbers and then returns the number, But the thing is that when it returns the number it has a decimal like this:

2.0

But I want it to give me:

2

so is there anyway I can do this?

2
  • 1
    Please show us your code.
    – martineau
    Jul 15, 2013 at 10:01
  • 3
    you can also use integer division with // that always give a int, for example 1//2 that give 0 Jan 5, 2016 at 15:46

8 Answers 8

71

You can call int() on the end result:

>>> int(2.0)
2
4
  • 3
    ValueError: invalid literal for int() with base 10: '68.0'
    – Liza
    Jul 11, 2015 at 4:32
  • 2
    @Kyrie You can do int(float('68.0'))
    – TerryA
    Jul 11, 2015 at 23:39
  • 5
    @TerryA It won't work in case of int(1.9999999999999999999), how can I just remove the decimals?
    – Bear
    Aug 24, 2016 at 17:59
  • 3
    @Bear math.floor(1.9999999999999999999)
    – hobbes3
    Feb 21, 2019 at 1:49
35

When a number as a decimal it is usually a float in Python.

If you want to remove the decimal and keep it an integer (int). You can call the int() method on it like so...

>>> int(2.0)
2

However, int rounds down so...

>>> int(2.9)
2

If you want to round to the nearest integer you can use round:

>>> round(2.9)
3.0
>>> round(2.4)
2.0

And then call int() on that:

>>> int(round(2.9))
3
>>> int(round(2.4))
2
3
  • 1
    What if it is a large number?
    – Jeff
    Apr 7, 2014 at 19:16
  • @Jeff Why don't you check yourself? Python does not have a problem with the size of numbers.
    – Inbar Rose
    Apr 9, 2014 at 6:49
  • Thanks! I realized that a bit after I posted. For whatever reason, I thought that a unix date format would be too large a number, but I was wrong!
    – Jeff
    Apr 9, 2014 at 14:13
3

You could probably do like below

# p and q are the numbers to be divided
if p//q==p/q:
    print(p//q)
else:
    print(p/q)
1
  • 1
    Thank you, this helped me solve a different but related issue. :) Jan 7, 2020 at 20:40
2

There is a math function modf() that will break this up as well.

import math

print("math.modf(3.14159) : ", math.modf(3.14159))

will output a tuple: math.modf(3.14159) : (0.14159, 3.0)

This is useful if you want to keep both the whole part and decimal for reference like:

decimal, whole = math.modf(3.14159)

1
>>> int(2.0)

You will get the answer as 2

1
  • 8
    What does this add that isn't already present in the other answers? Jan 5, 2016 at 14:15
1
def division(a, b):
    return a / b if a % b else a // b
0
1
if val % 1 == 0:
    val = int(val)

else:
    val = float(val)

This worked for me.

How it works: if the remainder of the quotient of val and 1 is 0, val has to be an integer and can, therefore, be declared to be int without having to worry about losing decimal numbers.

Compare these two situations:

A:

val = 12.00

if val % 1 == 0:
    val = int(val)
else:
    val = float(val)

print(val)

In this scenario, the output is 12, because 12.00 divided by 1 has the remainder of 0. With this information we know, that val doesn't have any decimals and we can declare val to be int.

B:

val = 13.58

if val % 1 == 0:
    val = int(val)
else:
    val = float(val)

print(val)

This time the output is 13.58, because when val is divided by 1 there is a remainder (0.58) and therefore val is declared to be a float.

By just declaring the number to be an int (without testing the remainder) decimal numbers will be cut off.

This way there are no zeros in the end and no other than the zeros will be ignored.

1
  • @jayveesea Thanks for the tip, I'm still new on the site and in programming overall.
    – Jerzy
    Jun 1, 2020 at 19:49
0

There are 4 ways to remove decimal places in Python,

  1. Using the int() function
  2. Using the trunc() function in math library
  3. Using string formatting and methods
    1. Using %d placeholder
    2. Using split() function
  4. Using the round() function

1. Using the int() function

int() is a Python Built-in Type function.

>>> number = 2.0
>>> number
2.0
>>> number = int(2.0)
2
>>> type(number)
<class 'int'>

2. Using the trunc() function in math library

trunc() function in math library.

>>> import math
>>> 
>>> number = 2.0
>>> number
2.0
>>> number = math.trunc(2.0)
2
>>> type(number)
<class 'int'>

3. Using string formatting and methods

Please note:- Everything returned in this section is of type str.

1. Using %d placeholder

Format the string with the %d placeholder

>>> number = 2.0
>>> number
2.0
>>> number = '%d'%number
>>> number
'2'
>>> type(number)
<class 'str'>

2. Using split() function

split() function is a string method.

>>> number = 2.0
>>> number
2.0
>>> number=str(number).split('.')[0]
>>> number
'2'
>>> type(number)
<class 'str'>

4. Using the round() function

round() is a Built-in Function.

>>> number = 2.0
>>> number
2.0
>>> number = round(number)
>>> number
2
>>> type(number)
<class 'int'>

References:-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.