Добавить в корзинуПозвонить
Найти в Дзене

Как отбросить дробную часть в python

In Python, "отбросить дробную часть" (discarding the fractional part) usually refers to Truncating a number, meaning removing everything after the decimal point and moving towards zero. There are a few ways to achieve this, depending on whether you want to round down, round up, or simply remove the decimal for both positive and negative numbers. Here are the most common and effective methods: 1. Using Math. trunc() This is the most direct way to Truncate a number. It simply removes the fractional part, moving towards zero. Python Import math Positive_num = 3.14159 Negative_num = -2.71828 Print(f"Original positive: {positive_num}, Truncated: {math. trunc(positive_num)}") # Output: 3 Print(f"Original negative: {negative_num}, Truncated: {math. trunc(negative_num)}") # Output: -2 2. Using Int() constructor When you convert a floating-point number to an integer using int(), Python truncates the decimal part, also moving towards zero. This works similarly to math. trunc(). Python Positive_n

In Python, "отбросить дробную часть" (discarding the fractional part) usually refers to Truncating a number, meaning removing everything after the decimal point and moving towards zero. There are a few ways to achieve this, depending on whether you want to round down, round up, or simply remove the decimal for both positive and negative numbers.

Here are the most common and effective methods:

1. Using Math. trunc()

This is the most direct way to Truncate a number. It simply removes the fractional part, moving towards zero.

Python

Import math

Positive_num = 3.14159

Negative_num = -2.71828

Print(f"Original positive: {positive_num}, Truncated: {math. trunc(positive_num)}") # Output: 3

Print(f"Original negative: {negative_num}, Truncated: {math. trunc(negative_num)}") # Output: -2

2. Using Int() constructor

When you convert a floating-point number to an integer using int(), Python truncates the decimal part, also moving towards zero. This works similarly to math. trunc().

Python

Positive_num = 3.14159

Negative_num = -2.71828

Print(f"Original positive: {positive_num}, As int: {int(positive_num)}") # Output: 3

Print(f"Original negative: {negative_num}, As int: {int(negative_num)}") # Output: -2

Note: While int() is concise, math. trunc() is often preferred for clarity when the explicit intent is truncation, as int() is a type conversion.

3. Using Math. floor() (Round Down)

If "отбросить дробную часть" specifically means to Round down to the nearest whole number, regardless of whether the number is positive or negative, then math. floor() is what you need. It always rounds towards negative infinity.

Python

Import math

Positive_num = 3.14159

Negative_num = -2.71828

Print(f"Original positive: {positive_num}, Floored: {math. floor(positive_num)}") # Output: 3

Print(f"Original negative: {negative_num}, Floored: {math. floor(negative_num)}") # Output: -3

Notice how math. floor() rounds -2.71828 down to -3, while math. trunc() and int() truncate it to -2.

4. Using Math. ceil() (Round Up)

If you need to Round up to the nearest whole number, you can use math. ceil(). It always rounds towards positive infinity.

Python

Import math

Positive_num = 3.14159

Negative_num = -2.71828

Print(f"Original positive: {positive_num}, Ceiled: {math. ceil(positive_num)}") # Output: 4

Print(f"Original negative: {negative_num}, Ceiled: {math. ceil(negative_num)}") # Output: -2

Which method to choose?

Use Math. trunc() or Int() if you simply want to Remove the decimal part and move the number closer to zero, regardless of its sign. Use Math. floor() if you want to Round down to the nearest whole number (towards negative infinity). Use Math. ceil() if you want to Round up to the nearest whole number (towards positive infinity).

Consider the behavior with negative numbers carefully, as trunc(), floor(), and ceil() produce different results for them.