How to remove the decimal in Python

This guide shows you how to remove decimals in Python. Learn different methods, tips, real-world uses, and how to debug common errors.

How to remove the decimal in Python
Published on: 
Tue
Mar 3, 2026
Updated on: 
Wed
Mar 4, 2026
The Replit Team Logo Image
The Replit Team

You often need to remove decimals in Python for data cleaning and presentation. Python offers built-in functions like int() to convert floating-point numbers into integers with precision and control.

Here, you'll find several techniques to handle decimals, complete with practical tips and real-world applications. You'll also get debugging advice to help you master these techniques in your projects.

Using int() to truncate decimals

num = 42.75
no_decimal = int(num)
print(f"Original: {num}, Without decimal: {no_decimal}")--OUTPUT--Original: 42.75, Without decimal: 42

The int() function offers the most direct way to remove a decimal. It doesn't round the number; it truncates it. This means it simply cuts off everything after the decimal point. As you can see in the example, int(42.75) becomes 42, not 43.

This method is fast and predictable, making it ideal when you only need the whole number part of a value—for instance, when you're categorizing data or performing calculations where fractional parts are irrelevant.

Basic rounding and truncating techniques

Beyond the straightforward truncation of int(), Python offers more sophisticated tools like math.floor(), math.ceil(), and round() for greater rounding precision.

Using math.floor() to round down

import math
num = 42.75
rounded_down = math.floor(num)
print(f"Original: {num}, Rounded down: {rounded_down}")--OUTPUT--Original: 42.75, Rounded down: 42

The math.floor() function always rounds a number down to the greatest integer that is less than or equal to the original value. While it behaves like int() for positive numbers, its true utility shines with negative numbers.

  • For instance, math.floor(-42.75) returns -43, whereas int(-42.75) would give you -42.

This makes it ideal for statistical or financial applications where consistently rounding down is a requirement.

Using math.ceil() to round up

import math
num = 42.75
rounded_up = math.ceil(num)
print(f"Original: {num}, Rounded up: {rounded_up}")--OUTPUT--Original: 42.75, Rounded up: 43

The math.ceil() function is the opposite of math.floor(), as it always rounds a number up to the next highest integer. Think of "ceil" as "ceiling"—it always goes up. For example, math.ceil(42.75) becomes 43.

  • Its behavior with negative numbers is distinct. math.ceil(-42.75) returns -42, since that's the integer greater than the original number.

This is useful when you need to ensure a value meets a minimum threshold, like calculating resources where any fraction requires a whole unit.

Using round() for nearest integer rounding

num = 42.75
rounded = round(num)
print(f"Original: {num}, Rounded: {rounded}")--OUTPUT--Original: 42.75, Rounded: 43

The round() function provides standard rounding to the nearest integer. As shown, round(42.75) correctly evaluates to 43 because it's closer to 43 than 42. This is often the most intuitive rounding method for general use.

It's important to know how round() handles numbers exactly halfway between two integers. Python 3 uses a "round half to even" strategy to avoid statistical bias in calculations.

  • For example, round(2.5) results in 2, but round(3.5) gives 4. The function rounds to the nearest even integer in these tie-breaking situations.

String formatting and specialized libraries

When mathematical functions aren't enough, string formatting and specialized libraries like Decimal and numpy offer more powerful ways to manage decimals.

Using string formatting to display without decimals

num = 42.75
formatted = f"{num:.0f}"
print(f"Original: {num}, Formatted: {formatted}")
print(f"Type of formatted result: {type(formatted)}")--OUTPUT--Original: 42.75, Formatted: 43
Type of formatted result: <class 'str'>

String formatting gives you fine-grained control over how numbers appear. With an f-string and the format specifier :.0f, you can round a float to zero decimal places. This method rounds the number based on standard rules, so 42.75 becomes 43.

  • Crucially, the result is a string, not a number, as confirmed by type(formatted).
  • This makes it ideal for presentation purposes, like in reports or UIs, but unsuitable for subsequent mathematical calculations.

Using the Decimal module for precise control

from decimal import Decimal, ROUND_DOWN
num = Decimal('42.75')
no_decimal = num.quantize(Decimal('1.'), rounding=ROUND_DOWN)
print(f"Original: {num}, Without decimal: {no_decimal}")--OUTPUT--Original: 42.75, Without decimal: 42

For applications demanding high precision, like finance, the Decimal module is your go-to. It avoids the floating-point inaccuracies that can sometimes trip you up. The quantize() method lets you reshape a number to a specific exponent—in this case, using Decimal('1.') sets it to zero decimal places.

  • You get explicit control over rounding with parameters like ROUND_DOWN, which truncates the value.
  • Notice the number is created from a string (Decimal('42.75')) to ensure its value is represented exactly, a key feature of the module.

Using numpy for vectorized decimal removal

import numpy as np
numbers = np.array([42.75, 33.25, 18.90, 7.33])
no_decimals = numbers.astype(int)
print(f"Original array: {numbers}")
print(f"Without decimals: {no_decimals}")--OUTPUT--Original array: [42.75 33.25 18.9 7.33]
Without decimals: [42 33 18 7]

When you're working with large datasets, numpy is incredibly efficient. It lets you perform operations on entire arrays at once, a process known as vectorization. In this example, the astype(int) method converts every number in the numpy array to an integer, effectively truncating the decimals.

  • This vectorized approach is much faster than looping through a standard Python list.
  • Like the basic int() function, astype(int) truncates the values; it doesn't round them.

Move faster with Replit

The techniques you've learned are building blocks for larger projects. With an AI-powered development platform like Replit, you can turn these concepts into complete applications using natural language. Describe what you want to build, and Replit Agent creates it—complete with databases, APIs, and deployment.

For the decimal removal methods we've explored, Replit Agent can turn them into production-ready tools:

  • Build a financial modeling tool that uses the Decimal module for precise currency calculations and reporting.
  • Create a data processing utility that uses numpy to efficiently truncate floating-point values across large datasets for analysis.
  • Deploy a simple unit converter that uses math.floor() and math.ceil() to correctly handle conversions between measurement systems.

You provide the concept; Replit Agent handles the implementation. Describe your app, and it will write the code, run tests, and fix bugs automatically. Try Replit Agent to turn your ideas into working software.

Common errors and challenges

Even with Python's powerful tools, you might run into a few common pitfalls when removing decimals from numbers.

  • Handling negative numbers with int() and math.floor(). The behavior of these functions diverges with negative values. While int() truncates toward zero (int(-9.9) becomes -9), math.floor() always rounds down to the nearest integer (math.floor(-9.9) becomes -10). This subtle difference can introduce bugs if you're not careful, especially in calculations where direction matters.
  • Avoiding type errors when mixing numeric operations. Remember that some methods, particularly string formatting, return a string, not a number. If you try to perform a mathematical operation on a formatted string like "43", you'll get a TypeError. Always be mindful of your variable's data type before using it in further calculations.
  • Dealing with precision issues in round(). The built-in round() function can sometimes surprise you. It uses a "round half to even" strategy, meaning numbers ending in .5 are rounded to the nearest even integer. For instance, round(2.5) is 2, while round(3.5) is 4. This isn't a flaw—it's a deliberate choice to reduce statistical bias—but it's a crucial detail to remember.

Handling negative numbers with int() and math.floor()

When working with negative numbers, the difference between int() and math.floor() becomes critical. The int() function truncates the decimal, effectively rounding toward zero. However, math.floor() always rounds down to the nearest integer, moving further into the negatives.

This subtle difference can easily introduce bugs. Check out the code below to see how int()'s truncation might not give you the "rounded down" result you're looking for.

negative_num = -42.75
truncated = int(negative_num)
print(f"Original: {negative_num}, Truncated with int(): {truncated}")
# This prints -42, which might not be the expected "rounded down" value

The int() function rounds toward zero, so -42.75 becomes -42. This is often not the intended "floor" value. See how to get the correct rounded-down result in the example below.

import math
negative_num = -42.75
truncated = int(negative_num) # Gives -42
floored = math.floor(negative_num) # Gives -43
print(f"Original: {negative_num}, Truncated with int(): {truncated}")
print(f"Original: {negative_num}, Rounded down with floor(): {floored}")

As the code demonstrates, int() and math.floor() behave differently with negative numbers. While int(-42.75) truncates toward zero to give you -42, math.floor(-42.75) correctly rounds down to -43. It's crucial to use math.floor() when you must consistently round down—especially in financial or statistical applications—to avoid unexpected calculation errors.

Avoiding type errors when mixing numeric operations

Mixing data types can introduce subtle bugs. While some operations throw a clear TypeError, others fail silently by losing data. Prematurely converting a float to an integer with int() is a common culprit, leading to incorrect calculations. See how this plays out in the following example.

price = 19.99
quantity = 3
total = int(price) * quantity
print(f"Total cost: ${total}") # Will print 57 instead of expected 59.97

By converting price with int() before multiplying, the calculation loses the decimal value entirely, leading to an incorrect total. The example below shows how to perform the calculation correctly to maintain precision.

price = 19.99
quantity = 3
total = price * quantity
truncated_total = int(total)
print(f"Full total: ${total:.2f}, Truncated total: ${truncated_total}")

To maintain precision, always complete your calculations before truncating the result. In the example, multiplying price by quantity first ensures the total is accurate.

Only then is int() used on the final total to remove the decimal. This approach prevents the premature data loss seen previously, where the decimal was dropped before the calculation. It's crucial for financial calculations or any scenario where precision matters.

Dealing with precision issues in round()

You might expect round() to be perfectly predictable, but floating-point inaccuracies can lead to surprising outcomes. This happens because some decimal numbers can't be stored with perfect precision. See how this plays out in the following code example.

num = 2.675
rounded = round(num, 2)
print(f"Original: {num}, Rounded to 2 decimal places: {rounded}") # Prints 2.67!

Because of how computers store floats, 2.675 is represented as a number just shy of its actual value, causing round() to round down. The following example demonstrates how to get consistently accurate results.

from decimal import Decimal, ROUND_HALF_UP
num = Decimal('2.675')
rounded = num.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
print(f"Original: {num}, Rounded to 2 decimal places: {rounded}")

To sidestep floating-point precision issues, use the Decimal module. By creating a Decimal object from a string, like Decimal('2.675'), you'll ensure the number is stored exactly as written. The quantize() method then lets you round to a specific number of decimal places with explicit rules, such as ROUND_HALF_UP. This approach guarantees predictable rounding, which is essential for financial calculations or any application where accuracy is critical.

Real-world applications

With an understanding of the potential pitfalls, you can apply these techniques to solve practical problems like calculating discounts or converting file sizes.

Using int() for calculating discount prices

When you need to display a sale price in whole dollars, the int() function is a quick way to truncate the cents from a calculated discount.

original_price = 79.99
discount_percent = 15
discounted_price = original_price * (1 - discount_percent / 100)
sale_price_whole_dollars = int(discounted_price)
print(f"Original: ${original_price}, With 15% discount: ${discounted_price:.2f}")
print(f"Sale price (whole dollars only): ${sale_price_whole_dollars}")

This example demonstrates how to calculate a discount and prepare the result for different display needs. The calculation original_price * (1 - discount_percent / 100) efficiently finds the final price after applying the 15% reduction.

  • The code creates two variables: the precise discounted_price and a simplified integer version, sale_price_whole_dollars, using the int() function.
  • This allows you to present the price in multiple ways, such as showing the exact cost ($67.99) alongside a more prominent, rounded-down sale tag ($67) in a user interface.

Converting file sizes with int() truncation

When converting file sizes from bytes to kilobytes or megabytes, you can use int() to truncate the result for a simpler, whole-number value.

def format_file_size(size_bytes):
if size_bytes < 1024:
return f"{int(size_bytes)} B"
elif size_bytes < 1024 * 1024:
kb = size_bytes / 1024
return f"{kb:.2f} KB ({int(kb)} KB truncated)"
else:
mb = size_bytes / (1024 * 1024)
return f"{mb:.2f} MB ({int(mb)} MB truncated)"

file_sizes = [256, 3500, 1048576]
for size in file_sizes:
print(f"{size} bytes = {format_file_size(size)}")

The format_file_size function makes raw byte counts more readable by converting them into kilobytes (KB) or megabytes (MB). It uses conditional logic to check the input size and select the appropriate unit.

  • For sizes under 1024, it returns the value in bytes.
  • For larger values, it calculates the equivalent in KB or MB.

The function provides two outputs: a precise value formatted to two decimal places and a simplified integer version created with int(). This gives you the flexibility to display either a detailed or a truncated file size.

Get started with Replit

Turn your knowledge into a real tool with Replit Agent. Describe what you want to build, like "a currency converter that rounds down with math.floor()" or "a data dashboard that truncates large numbers for display."

Replit Agent writes the code, tests for errors, and deploys your app. You provide the idea, and it handles the rest. Start building with Replit.

Get started free

Create and deploy websites, automations, internal tools, data pipelines and more in any programming language without setup, downloads or extra tools. All in a single cloud workspace with AI built in.

Get started for free

Create & deploy websites, automations, internal tools, data pipelines and more in any programming language without setup, downloads or extra tools. All in a single cloud workspace with AI built in.