{"id":7274,"date":"2021-09-30T10:00:30","date_gmt":"2021-09-30T04:30:30","guid":{"rendered":"https:\/\/python-programs.com\/?p=7274"},"modified":"2021-11-22T18:39:24","modified_gmt":"2021-11-22T13:09:24","slug":"program-to-handle-precision-values-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/program-to-handle-precision-values-in-python\/","title":{"rendered":"Program to Handle Precision Values in Python"},"content":{"rendered":"
Beginners and experienced programmers can rely on these Best Java Programs Examples<\/a> and code various basic and complex logics in the Java programming language with ease.<\/p>\n Python treats every number with a decimal point as a double precision floating point number by default. The Decimal is a form of floating decimal point with more precision and a narrower range than the float. It is ideal for monetary and financial calculations. It is also more similar to how humans operate with numbers.<\/p>\n In contrast to hardware-based binary floating point, the decimal module features user-adjustable precision that can be as large as required for a given situation. The precision is set to 28 places by default.<\/p>\n Some values cannot be represented exactly in a float data format. For example, saving the 0.1 value in the float (binary floating point value) variable gives just an approximation of the value. Similarly, the 1\/5\u00a0number cannot be precisely expressed in decimal floating point format. Handling precision values in Python:<\/strong><\/p>\n We frequently encounter circumstances in which we process integer or numeric data into any application or manipulation procedures, regardless of programming language. We find data with decimal values using the same procedure. This is when we must deal with accuracy values.<\/p>\n Python has a number of functions for dealing with accuracy values for numeric data. It allows us to exclude decimal points or have customized values based on the position of the decimal values in the number.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to handle the precision values in python some of them are:<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n We may format the number as well as specify precision boundaries with the \u2018 %\u00a0‘ operator. This allows us to customize the precise point restrictions that are included in the final number.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n Below is the implementation:<\/b><\/p>\n Output:<\/strong><\/p>\n We can use the format() function to set limits for precision values, just like we can with the percent operator. We format the data as a string and set the limits for the points to be included after the decimal portion of the number with the format() function.<\/p>\n Syntax:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can extract and display integer values in a customized format using the Python round() function. As a check for precision handling, we can select the number of digits to be displayed after the decimal point.<\/p>\n Syntax:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Aside from the functions listed above, Python also provides us with a math module that contains a set of functions for dealing with precision values.<\/p>\n The Python math module includes the following functions for dealing with precision values\u2013<\/p>\n 1)trunc() function<\/strong> The trunc() function terminates all digits after the decimal points. That is, it only returns the digits preceding the decimal point.<\/p>\n Syntax:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can round off decimal numbers to the nearest high or low value using the ceil() and floor() functions.<\/p>\n The ceil() function takes the decimal number and rounds it up to the next large number after it. The floor() function, on the other hand, rounds the value to the next lowest value before it.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease. Python treats every number with a decimal point as a double precision floating point number by default. The Decimal is a form of floating decimal point with more precision …<\/p>\n
\nNeither type is ideal\u00a0in general, decimal types are better suited for financial and monetary computations, while double\/float types are better suited for scientific calculations.<\/p>\ngiven_number = 2345.1347216482926\u00a0 \u00a0 precisionlimit=4<\/pre>\n
the given number upto 4 decimal places 2345.1347<\/pre>\n
given_number = 2345.13\u00a0 \u00a0 precisionlimit=4<\/pre>\n
the given number upto 4 decimal places 2345.1300<\/pre>\n
Code for Handling Precision Values in Python<\/h2>\n
\n
Method #1:Using % operator<\/h3>\n
'%.point'%number<\/code><\/p>\n
point: It denotes the number of points we want after the decimal in the integer.\r\nnumber : The integer value to be worked on is represented by the number.<\/pre>\n
# given number\r\ngiven_number = 2345.1347216482926\r\n# using % operator and printing upto 4 decimal places\r\nans = '%.4f' % given_number\r\n# print the answer\r\nprint(\"the given number upto 4 decimal places\", ans)\r\n<\/pre>\n
the given number upto 4 decimal places 2345.1347<\/pre>\n
Method #2:Using format function<\/h3>\n
print (\"{0:.pointf}\".format(number)) <\/code><\/p>\n
# given number\r\ngiven_number = 2345.1336327777377\r\n# using % operator and printing upto 5 decimal places\r\nans = '{0:.5f}' .format(given_number)\r\n# print the answer\r\nprint(\"the given number upto 5 decimal places\", ans)\r\n<\/pre>\n
the given number upto 5 decimal places 2345.13363<\/pre>\n
Method #3:Using round() function<\/h3>\n
round(number, point)<\/code><\/p>\n
# given number\r\ngiven_number = 2345.1336327777377\r\n# using % operator and printing upto 4 decimal places\r\nans = round(given_number, 5)\r\n# print the answer\r\nprint(\"the given number upto 5 decimal places\", ans)\r\n<\/pre>\n
the given number upto 5 decimal places 2345.13363<\/pre>\n
4)Math functions in Python to handle precision values<\/h3>\n
\n2)The ceil() and floor() functions in Python<\/strong>
\nLet us go over them one by one.<\/p>\nMethod #4:Using trunc() function<\/h3>\n
import math\r\nmath.trunc(given number)<\/pre>\n
import math\r\n# given number\r\ngiven_number = 39245.1336327777377\r\n# truncating all the digits of given number\r\ntruncnumber = math.trunc(given_number)\r\n# print the answer\r\nprint(\"the given number after truncating digits\", truncnumber)\r\n<\/pre>\n
the given number after truncating digits 39245<\/pre>\n
Method #5:Using ceil() and floor() functions<\/h3>\n
import math\r\ngiven_number = 3246.3421\r\n\r\n# prining the ceil value of the given number\r\nprint('printing ceil value of the given numnber',\r\n given_number, '=', math.ceil(given_number))\r\n\r\n# prining the floor value of the given number\r\nprint('printing floor value of the given numnber',\r\n given_number, '=', math.floor(given_number))\r\n<\/pre>\n
printing ceil value of the given numnber 3246.3421 = 3247\r\nprinting floor value of the given numnber 3246.3421 = 3246<\/pre>\n
\n