![]() |
VOOZH | about |
We have to remove all decimals from a number and get the required output using Python. There are several ways to achieve this, depending on whether you want to simply truncate the decimal part, round it or perform other manipulations. For example, number is 12.345 then result is 12.
int( ) is a built-in function used to convert any value into an integer number.
Number1 = 44 Number2 = 856 Number3 = 9999 <class 'float'> <class 'int'>
Explanation: This code converts floating-point numbers to integers using the int() function, which removes the decimal part. It then prints the converted values and shows the data types of the original and converted numbers using type().
math( ) module is a standard built-in module in python. There are numerous mathematical functions defined in math( ) module. To use the truncate function, first, the math module must be imported, using trunc( ) function without defining the math( ) module gives an error. By using math.trunc( ) function a number can be truncated in python.
450 999998 -89 0
Explanation:
The split( ) function only works on strings. Hence, the decimal values are converted to a string using str( ) function and then split at the decimal point. The values remain in the string data type after performing split( ) function. Therefore, the values are again converted to the integer data type.
[998, 56, 25, -52]
Explanation:
round() function rounds the number to the nearest integer. By default, it rounds to the nearest integer and it rounds halfway cases (like 12.5) to the nearest even number.
13
Explanation: The round() function rounds 12.675 to the nearest integer. Since Python rounds halfway values to the nearest even number (also known as "bankers' rounding"), 12.675 rounds to 12.