Python remove decimal without rounding

Im a little new into python, and i wanted to test it out, my idea was to make a script, that would see how much stuff you could buy for a certain amount of money. The problem with this project though, is that i dont know remove the decimals, just like you like if you had 1,99 dollar and a soda costed 2 dollar, you technically wouldn't have enough money for it. Here is my script:

Banana = 1
Apple = 2
Cookie = 5

money = input("How much money have you got? ")
if int(money) >= 1:
    print("For ", money," dollars you can get ",int(money)/int(Banana),"bananas")
if int(money) >= 2:
    print("Or ", int(money)/int(Apple), "apples")
if int(money) >= 5:
    print("Or ", int(money)/int(Cookie)," cookies")
else:
    print("You don't have enough money for any other imported elements in the script")

Now, if i enter for example, 9 in this script, it will say i can get 1.8 cookies, how do i make it say i can get 1 cookies when entering fx 9?

The int() function converts a number or string into an integer. During that process Python throws away the decimal part of the value.,Another truncation option is the int() function. This function converts the argument we give it into an integer. During that process the value’s fractional portion is thrown away. So for floating-point values (and floats represented as strings), int() truncates the value towards zero (Python Docs, n.d. b).,The math.trunc() function returns the truncated integer from the numerical argument we give it (Python Docs, n.d. a). That truncates the fractional part of the number towards zero (Lutz, 2013). The result: we strip away the decimal portion of a number.,The other option is the int() function. When we give this function one argument, it truncates that argument into a whole number. When we got a floating-point value expressed as a string (such as "2.984"), then int() first converts that value to a number and then removes any fractional value.

import math

math.trunc(-10.90)
# Returns - 10

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Truncate values(
   throw away the fractional part)
truncA = math.trunc(valueA)
truncB = math.trunc(valueB)
truncC = math.trunc(valueC)
truncD = math.trunc(valueD)
truncE = math.trunc(valueE)

# Output the results
print(valueA, "truncated =", truncA)
print(valueB, "truncated =", truncB)
print(valueC, "truncated =", truncC)
print(valueD, "truncated =", truncD)
print(valueE, "truncated =", truncE)

1._

import math

math.trunc(-10.90)
# Returns - 10

2._

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Truncate values(
   throw away the fractional part)
truncA = math.trunc(valueA)
truncB = math.trunc(valueB)
truncC = math.trunc(valueC)
truncD = math.trunc(valueD)
truncE = math.trunc(valueE)

# Output the results
print(valueA, "truncated =", truncA)
print(valueB, "truncated =", truncB)
print(valueC, "truncated =", truncC)
print(valueD, "truncated =", truncD)
print(valueE, "truncated =", truncE)

The last part of the program outputs the results. With Python’s print() function we display the original value as well as the truncated one. Here’s how that output looks:

11.282985 truncated = 11
19.2545879 truncated = 19
0.50000001 truncated = 0
34.6403001 truncated = 34 -
   9.9121138 truncated = -9

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Truncate values to a whole integer(discard decimal part)
truncA = int(valueA)
truncB = int(valueB)
truncC = int(valueC)
truncD = int(valueD)
truncE = int(valueE)

# Print the results
print(valueA, "truncated =", truncA)
print(valueB, "truncated =", truncB)
print(valueC, "truncated =", truncC)
print(valueD, "truncated =", truncD)
print(valueE, "truncated =", truncE)

import math

# Some floating point values
values = [
   3.464, 6.708, 11.045,
   15.297, 21.213, 49.871
]

# Make a new list, with every value truncated
truncValues = [math.trunc(value) for value in values]

# Output both lists
print("Original values:\n", values)
print("Truncated values:\n", truncValues)


So in Python 3, there is an integer division operator // you can use:

Updated code:

import math
Banana = 1
Apple = 2
Cookie = 5

money = input("How much money have you got? ")
if int(money) >= 1:
   print("For ", money, " dollars you can get ", math.floor(int(money) / int(Banana)), "bananas")
if int(money) >= 2:
   print("Or ", math.floor(int(money) / int(Apple)), "apples")
if int(money) >= 5:
   print("Or ", math.floor(int(money) / int(Cookie)), " cookies")
else :
   print("You don't have enough money for any other imported elements in the script")


Last Updated : 25 Feb, 2021,GATE CS 2021 Syllabus

Output :

Number1 = 44
Number2 = 856
Number3 = 9999
<class 'float'>
   <class 'int'>


The decimal module provides support for fast correctly rounded decimal floating point arithmetic. It offers several advantages over the float datatype:,The number system for the decimal module provides special values including NaN, sNaN, -Infinity, Infinity, and two zeros, +0 and -0.,Decimal instances can be constructed from integers, strings, floats, or tuples. Construction from an integer or a float performs an exact conversion of the value of that integer or float. Decimal numbers include special values such as NaN which stands for “Not a number”, positive and negative Infinity, and -0:,A decimal number is immutable. It has a sign, coefficient digits, and an exponent. To preserve significance, the coefficient digits do not truncate trailing zeros. Decimals also include special values such as Infinity, -Infinity, and NaN. The standard also differentiates -0 from +0.

>>> from decimal
import *
>>>
getcontext().prec = 6 >>>
   Decimal(1) / Decimal(7)
Decimal('0.142857') >>>
   getcontext().prec = 28 >>>
   Decimal(1) / Decimal(7)
Decimal('0.1428571428571428571428571429')

>>> from decimal
import *
>>>
getcontext()
Context(prec = 28, rounding = ROUND_HALF_EVEN, Emin = -999999, Emax = 999999,
      capitals = 1, clamp = 0, flags = [], traps = [Overflow, DivisionByZero,
         InvalidOperation
      ])

   >>>
   getcontext().prec = 7 # Set a new precision

>>> getcontext().prec = 28 >>>
   Decimal(10)
Decimal('10') >>>
   Decimal('3.14')
Decimal('3.14') >>>
   Decimal(3.14)
Decimal('3.140000000000000124344978758017532527446746826171875') >>>
   Decimal((0, (3, 1, 4), -2))
Decimal('3.14') >>>
   Decimal(str(2.0 ** 0.5))
Decimal('1.4142135623730951') >>>
   Decimal(2) ** Decimal('0.5')
Decimal('1.414213562373095048801688724') >>>
   Decimal('NaN')
Decimal('NaN') >>>
   Decimal('-Infinity')
Decimal('-Infinity')

>>> c = getcontext()
>>> c.traps[FloatOperation] = True
>>> Decimal(3.14)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
      decimal.FloatOperation: [<class 'decimal.FloatOperation'>]
         >>> Decimal('3.5') < 3.7 Traceback (most recent call last): File "<stdin>" , line 1, in <module>
            decimal.FloatOperation: [<class 'decimal.FloatOperation'>]
               >>> Decimal('3.5') == 3.5
               True

>>> getcontext().prec = 6 >>>
   Decimal('3.0')
Decimal('3.0') >>>
   Decimal('3.1415926535')
Decimal('3.1415926535') >>>
   Decimal('3.1415926535') + Decimal('2.7182818285')
Decimal('5.85987') >>>
   getcontext().rounding = ROUND_UP >>>
   Decimal('3.1415926535') + Decimal('2.7182818285')
Decimal('5.85988')

>>> Decimal("1e9999999999999999999")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
      decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]


How do you get rid of decimals in Python?

To remove the decimal from a number, we can use the int() method in Python. The int() method takes the number as an argument and returns the integer by removing the decimal part from it. It can be also used with negative numbers.

How do I stop Python from rounding?

It is simply always showing a fixed number of significant digits. Try import math; p=3.14; print p; p=math. pi; print p .

How do I turn a decimal into a whole number in Python?

How to convert a string with decimals to an integer in Python.
a_string = "1.33".
float_str = float(a_string).
int_str = int(float_str).
print(int_str).