How to remove the decimal in Python

Learn how to remove decimals in Python. This guide covers various methods, tips, real-world applications, and common error debugging.

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

Python developers often need to remove decimals from numbers to clean data or format output. The language provides simple, built-in functions to handle this conversion efficiently.

In this article, you'll explore techniques to remove decimals. These range from functions like int() and trunc() for simple conversion to the math module for more control. You'll get practical tips, see real-world applications, and receive debugging advice.

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 a straightforward way to remove a decimal by truncating the number. This means it simply cuts off everything after the decimal point without any rounding. In the example, when int() is applied to 42.75, it discards the .75 and returns the integer 42.

This approach is fast and effective, especially when you need to isolate the whole number part of a float for calculations or formatting. It consistently truncates toward zero, making its behavior predictable for both positive and negative numbers.

Basic rounding and truncating techniques

While int() simply truncates, Python's math module and the built-in round() function give you more precise control over how decimals are handled.

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 nearest whole number. It's different from int(), which simply truncates. For a positive number like 42.75, both functions return 42, but their behavior diverges with negative numbers.

  • math.floor(-42.75) returns -43.
  • int(-42.75) returns -42.

This distinction makes math.floor() ideal when you need to consistently round down, regardless of a number's sign.

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 counterpart to math.floor(), as it always rounds a number up to the next whole number. Think of it as finding the "ceiling" for a given float. For example, it converts 42.75 to 43.

  • With a positive number, math.ceil(42.75) returns 43.
  • With a negative number, math.ceil(-42.75) returns -42.

This makes math.ceil() perfect for scenarios where you must round up to meet a minimum integer requirement.

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 built-in round() function rounds a number to the nearest integer. Unlike functions that always round up or down, it chooses the whole number that's mathematically closest. For instance, round(42.75) returns 43 because it's nearer to 43 than 42.

  • It's important to know how round() handles numbers ending in .5. Python 3 uses a "round half to even" strategy.
  • This means it rounds to the nearest even integer, so round(2.5) becomes 2, while round(3.5) becomes 4.

String formatting and specialized libraries

When standard rounding isn't enough, string formatting and libraries like Decimal and numpy provide more tailored solutions for removing 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 lets you control how numbers appear without changing their underlying value. The f-string format specifier :.0f tells Python to display a number with zero decimal places, which also rounds it to the nearest whole number.

  • The expression f"{num:.0f}" rounds 42.75 to 43.
  • It's important to note that the result is a string (str), not a number. This makes the technique ideal for formatting output for display, but not for further 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

The Decimal module is your go-to for high-precision arithmetic, like in financial applications, where standard floats can introduce small errors. By creating a number from a string, such as Decimal('42.75'), you preserve its exact value.

You can then use the quantize() method to reshape the number. Passing Decimal('1.') sets the format to a whole number, and the rounding=ROUND_DOWN argument explicitly tells it to truncate the value—similar to int() but with the precision benefits of the Decimal type.

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]

For tasks involving lists or arrays of numbers, numpy offers a powerful, vectorized approach. This means you can apply an operation to every item in an array simultaneously—a method that's much faster than a traditional loop.

  • The astype(int) method is the key here. It recasts the entire array to the integer data type.
  • This effectively truncates the decimal from each number, similar to how the int() function works on a single value.

Move faster with Replit

Replit is an AI-powered development platform where you can start coding Python instantly. It comes with all dependencies pre-installed, so you can skip the setup and focus on building.

While knowing how to use functions like int() and math.floor() is useful, building a complete application is the next step. With Agent 4, you can move from piecing together techniques to creating a working product from a simple description.

  • A currency converter that takes a float and returns a rounded whole number for quick estimates.
  • A data processing tool that cleans raw sensor data by truncating all decimal values before storage.
  • A simple budget tracker that calculates daily spending and displays it without cents for a clean overview.

Simply describe your app, and Replit will write the code, test it, and fix issues automatically, all within your browser.

Common errors and challenges

When removing decimals, you might run into a few common pitfalls with negative numbers, data types, and rounding behavior.

A frequent point of confusion is how int() and math.floor() treat negative numbers. Because int() truncates toward zero, int(-42.75) becomes -42. In contrast, math.floor() always rounds down to the next lowest integer, so math.floor(-42.75) results in -43. Choosing the wrong function can introduce subtle bugs, especially in calculations where directional rounding is important.

Another common mistake is attempting mathematical operations on a number that has been converted to a string for display. Methods like f-string formatting produce a string, not a number. If you try to use this string in a calculation, Python will raise a TypeError. You must convert the value back to a numeric type if you need to perform more math.

Python's built-in round() function can also be a source of confusion due to its "round half to even" strategy. For example, round(2.5) evaluates to 2, while round(3.5) becomes 4. This approach is designed to reduce statistical bias in large datasets, but it can be unexpected if you're used to always rounding .5 values up. If your logic depends on a different rounding rule, you'll need a more specialized approach.

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

The distinction between int() and math.floor() is most apparent with negative numbers. Since int() truncates toward zero and math.floor() always rounds down, choosing the wrong one can introduce subtle bugs. The code below highlights this difference in action.

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

Using int() on a negative number moves it toward zero, returning -42. This result is often unexpected if you need to strictly round down. See how to get the intended result in the code 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}")

The code highlights a key difference when handling negative numbers. While int() truncates toward zero, math.floor() always rounds down.

  • int(-42.75) returns -42.
  • math.floor(-42.75) returns -43.

This distinction is crucial in contexts like financial modeling or data analysis where directional rounding can't be ambiguous. Always confirm you're using the right function for negative values to prevent logic errors.

Avoiding type errors when mixing numeric operations

Converting a number to an integer too early can cause silent logical errors. When you truncate a value with int() before a calculation is finished, you risk losing important decimal data, leading to incorrect results. The code below demonstrates this pitfall.

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

The code truncates price to 19 before multiplying by quantity. This premature conversion loses the .99 from each item, leading to an inaccurate total. Check out the corrected order of operations in the next example.

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

The corrected code performs the multiplication first, preserving the decimal values during the calculation. By calculating price * quantity before applying int(), you ensure the total is accurate before it's truncated. This simple reordering prevents silent bugs.

  • Always complete all mathematical operations before removing decimals to maintain precision, especially in financial contexts where accuracy is critical.

Dealing with precision issues in round()

The round() function can sometimes produce surprising results due to how computers store floating-point numbers. Because of tiny precision errors in binary representation, a number you expect to round up might round down. The code below demonstrates this unexpected behavior.

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

Because of how floats are stored, the number 2.675 is represented internally as a value slightly smaller than itself. This causes round() to round down unexpectedly. The code below shows how to get the correct result.

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 fix floating-point precision errors, use the Decimal module. Creating a number from a string, like Decimal('2.675'), ensures you're working with its exact value, not a binary approximation.

The quantize() method then rounds it precisely. Using the ROUND_HALF_UP argument forces values ending in 5 to round up, giving you predictable behavior. This approach is essential for financial applications or any scenario where rounding accuracy can't be compromised.

Real-world applications

Understanding how to avoid common errors makes it easier to apply these techniques to real-world tasks like calculating discounts or formatting file sizes.

Using int() for calculating discount prices

Applying int() after calculating a discount is a common way to present a simplified sale price by removing the cents.

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 shows how to calculate a final price and then simplify it for display. The code first computes the discounted_price with full decimal precision. Only after the calculation is complete does it use int() to truncate the result, creating sale_price_whole_dollars.

  • The multiplication happens first, ensuring the discount is applied accurately.
  • int() then strips the cents, leaving just the whole dollar amount.

This order is crucial; truncating the price before the calculation would produce an incorrect final value.

Converting file sizes with int() truncation

Truncating file sizes with int() is a common practice for creating user-friendly displays that show an approximate size without decimal clutter.

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)}")

This format_file_size function makes raw byte counts more readable by converting them to kilobytes (KB) or megabytes (MB). It uses conditional logic to check the size and apply the right conversion.

  • For sizes under 1024 bytes, it returns the number as is.
  • For larger files, it calculates the KB or MB equivalent by dividing by 1024.
  • The function then returns a string showing both the precise value to two decimal places and a simplified, truncated version created with int().

Get started with Replit

Now, turn these techniques into a real tool. Tell Replit Agent to "build a currency converter that rounds to the nearest whole dollar" or "create a data cleaner that truncates all float values in a CSV."

Replit Agent writes the code, tests for errors, and deploys your app from a simple description. Start building with Replit.

Build your first app today

Describe what you want to build, and Replit Agent writes the code, handles the infrastructure, and ships it live. Go from idea to real product, all in your browser.

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.