How to perform calculations with fractions or number's in Python
Performing mathematical calculations with integers and floating-point numbers is easy in Python.
Rounding Numerical Values:
Problem: You want to round a floating-point number to a fixed number of decimal places.
Solution: For simple rounding, use the built-in round(value, ndigits) function.
When a value is exactly halfway between two choices, the behavior of round is to round to the nearest even digit. That is the values such as 1.5 or 2.5 both get rounded to 2.
The number of digits given to round() can be negative, in which case rounding takes a place for tens, hundreds, thousands, and so on.
For example :
a = 1627731
print( round(a, -1))
print(round(a, -2))
print(round(a, -3))
Output:
1627730
1627700
1628000
Don’t confuse rounding with formatting a value for output. If your goal is simply to output a numerical value with a certain number of decimal places, you don’t typically need to use round(). Instead, just specify the desired precision when formatting.
For example:
Performing Accurate Decimal Calculation:
Problem: You need to perform accurate calculations with decimal numbers, and don’t want the
small errors that naturally occur with floats.
Solution: If you want more accuracy (and are willing to give up some performance), you can use
the decimal module:
Formatting Numbers for Output:
Problem: You need to format a number for output, controlling the number of digits, alignment,
inclusion of a thousand separators, and other details.
Solution: To format a single number for output, use the built-in format() function. If you want to use exponential notation, change the f to an e or E, depending on the case you want to use for the exponential specifier.
For example:
Working with Binary, Octal, and Hexadecimal Integer:
Problem: You need to convert or output integers represented by binary, octal, or hexadecimal
digits.
Solution: To convert an integer into a binary, octal, or hexadecimal text string, use the bin(),
oct(), or hex() functions. Alternatively, you can use the format() function if you don’t want the 0b, 0o, or 0x prefixes to appear.
Performing Complex-Valued Math:
Problem: You just need to perform some calculations using complex numbers.
Solution: Complex numbers can be specified using the complex(real, imag) function or by
floating-point numbers with a j suffix. The real, imaginary, and conjugate values are easy to obtain.
Conclusion: In this article, we learn how to perform calculations with fractions or numbers in python.
Thank you very much for reading.😊 Please read other articles.
Comments
Post a Comment
If you have any doubt, please let me know. To check my other blog kindly check the following links:
https://pythoholic.blogspot.com/
If you are interested in reading Marathi stories and other stuff, kindly check the following link.
https://pratilipi.page.link/q8dZ4ffZwKPHUx6R9
ꜰᴏʀ ᴇxᴘʟᴏʀɪɴɢ ᴛʜᴇ ᴡᴏʀʟᴅ ᴘʟᴇᴀꜱᴇ ʜᴀᴠᴇ ʟᴏᴏᴋ ᴀɴᴅ ꜰᴏʟʟᴏᴡ.
https://maps.app.goo.gl/jnKyzdDpKMFutUqR7