{"id":8141,"date":"2023-06-29T05:00:17","date_gmt":"2023-06-28T23:30:17","guid":{"rendered":"https:\/\/tutorpython.com\/?p=8141"},"modified":"2023-10-19T15:17:17","modified_gmt":"2023-10-19T09:47:17","slug":"math-trunc-python","status":"publish","type":"post","link":"https:\/\/tutorpython.com\/math-trunc-python","title":{"rendered":"math.trunc Python | Math Trunc Method"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">In This Article<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/tutorpython.com\/math-trunc-python\/#Python_mathtrunc_method_introduction\" >Python math.trunc method introduction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/tutorpython.com\/math-trunc-python\/#Math_Trunc_Python_Example_and_Uses\" >Math Trunc Python Example and Uses<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/tutorpython.com\/math-trunc-python\/#Truncating_a_Positive_Float_Number_%E2%80%93_Using_mathtrunc_in_Python\" >Truncating a Positive Float Number &#8211; Using math.trunc in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/tutorpython.com\/math-trunc-python\/#Truncating_a_Negative_Float_Number_%E2%80%93_Using_mathtrunc_Python\" >Truncating a Negative Float Number &#8211; Using math.trunc Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/tutorpython.com\/math-trunc-python\/#Truncating_a_Complex_Number_%E2%80%93_Using_Python_math_trunc\" >Truncating a Complex Number &#8211; Using Python math trunc<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/tutorpython.com\/math-trunc-python\/#Heres_an_example_of_how_we_can_define_such_a_function_in_Python\" >Here&#8217;s an example of how we can define such a function in Python:<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/tutorpython.com\/math-trunc-python\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p>Known for its versatility and extensive library support, Python provides programmers with a wide range of <a href=\"https:\/\/tutorpython.com\/tutorial\/functions-in-python\" target=\"_blank\" rel=\"noopener\">mathematical functions<\/a> and methods through its built-in <a href=\"https:\/\/tutorpython.com\/tutorial\/modules-in-python\" target=\"_blank\" rel=\"noopener\"><code>math<\/code> module<\/a>. One method that has proven to be very useful is the <code>math.trunc()<\/code> function in Python programming. This function allows developers to<strong> truncate the fractional part of a <a href=\"https:\/\/tutorpython.com\/tutorial\/numeric-data-type-conversions-in-python\" target=\"_blank\" rel=\"noopener\">floating-point number<\/a>, thereby obtaining its integer value<\/strong>.<\/p>\n<p>Whether you need exact <a href=\"https:\/\/tutorpython.com\/tutorial\/data-types-in-python\" target=\"_blank\" rel=\"noopener\">integer values<\/a> \u200b\u200bfor calculations, data manipulation, or formatting, the <code>math.trunc()<\/code> method can help.<\/p>\n<p>In this article, we take a deep dive into the <code>math.trunc()<\/code> Python method, covering its <a href=\"https:\/\/tutorpython.com\/tutorial\/python-syntax\" target=\"_blank\" rel=\"noopener\">syntax<\/a>, purpose, and exploring practical use cases that highlight its importance in programming. By understanding and learning this method, you will have a powerful tool for working efficiently with <a href=\"https:\/\/tutorpython.com\/tutorial\/data-types-in-python\" target=\"_blank\" rel=\"noopener\">floating point numbers<\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Python_mathtrunc_method_introduction\"><\/span>Python math.trunc method introduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>math.trunc()<\/code> method, belong to the <code>math<\/code> module in Python, allows us to truncate the fractional or decimal part of a floating-point number and retrieve its integer component. By discarding the decimal portion, the method essentially returns the largest integer that is <a href=\"https:\/\/tutorpython.com\/tutorial\/types-of-operators-in-python\" target=\"_blank\" rel=\"noopener\">less than or equal<\/a> to the given number.<\/p>\n<p>To utilize the <code>math.trunc()<\/code> method effectively, we follow a specific syntax as outlined below:<\/p>\n<pre><code>import math\r\nmath.trunc(number)<\/code><\/pre>\n<p>We begin by importing the <code>math<\/code> module, which provides us with access to various mathematical functions and methods.<\/p>\n<p>Next, we invoke the <code>trunc()<\/code> method from the <code>math<\/code> module, <a href=\"https:\/\/tutorpython.com\/tutorial\/pass-statement-in-python\" target=\"_blank\" rel=\"noopener\">passing<\/a> the desired floating-point value as the argument. The <code>number<\/code> parameter represents the specific floating-point value that we wish to truncate.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Math_Trunc_Python_Example_and_Uses\"><\/span>Math Trunc Python Example and Uses<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In the sections that follow, we will showcase various examples of the use of <code>math.trunc()<\/code>.<\/p>\n<p>Each example demonstrates a unique case where the <code>math.trunc()<\/code> method can be employed to obtain the integer component of a given value.<\/p>\n<p>Let&#8217;s begin our exploration.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Truncating_a_Positive_Float_Number_%E2%80%93_Using_mathtrunc_in_Python\"><\/span>Truncating a Positive Float Number &#8211; Using math.trunc in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In this example, we will demonstrate how the <code>math.trunc()<\/code> method can be used to truncate a positive floating-point number and extract its integer component.<\/p>\n<p>Consider a scenario where we have a positive float number, such as <code>3.14159<\/code>, and we want to obtain the integer part by discarding the decimal portion. See the code snippet below:<\/p>\n<pre class=\"language-python line-numbers\"><code>import math\r\nfloat_number = 3.14159\r\ntruncated_number = math.trunc(float_number)\r\nprint(truncated_number)    # prints 3\r\n<\/code><\/pre>\n<p>We first import the <code>math<\/code> module to access the <code>math.trunc()<\/code> method. Next, we initialize the variable <code>float_number<\/code> with the positive float value we want to truncate, which is <code>3.14159<\/code> in this case. Then, we <a href=\"https:\/\/tutorpython.com\/tutorial\/function-arguments-in-python\" target=\"_blank\" rel=\"noopener\">pass this variable<\/a> as an argument to the <code>math.trunc()<\/code> method, which returns the truncated integer value. Finally, we print the result, which would be <code>3<\/code>.<\/p>\n<p>Now that we have seen how to truncate a positive float number, let&#8217;s move on to the next example, where we will discuss truncating a negative float number.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Truncating_a_Negative_Float_Number_%E2%80%93_Using_mathtrunc_Python\"><\/span>Truncating a Negative Float Number &#8211; Using math.trunc Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In this example, we will focus on truncating a negative floating-point number using the <code>math.trunc()<\/code> method. When dealing with negative float numbers, <code>math.trunc()<\/code> simply discard the fractional part, and give us the integer part.<\/p>\n<p>Consider the code snippet below, which demonstrates how to truncate a negative float number using the <code>math.trunc()<\/code> method:<\/p>\n<pre class=\"language-python line-numbers\"><code>import math\r\nfloat_number = -2.71828\r\ntruncated_number = math.trunc(float_number)\r\nprint(truncated_number)    # prints -2\r\n<\/code><\/pre>\n<p>The negative value we want to truncate is <code>-2.71828<\/code> . When we assign it to the variable <code>float_number<\/code> , and then pass this variable as an argument to <code>math.trunc()<\/code>, we get the truncated negative integer that would be <code>-2<\/code>.<\/p>\n<p>Now that we are done with truncating a negative float number, let&#8217;s proceed to an example of truncating a <a href=\"https:\/\/www.khanacademy.org\/math\/algebra2\/x2ec2f6f830c9fb89:complex\/x2ec2f6f830c9fb89:complex-num\/a\/intro-to-complex-numbers\" target=\"_blank\" rel=\"nofollow noopener\">complex number<\/a>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Truncating_a_Complex_Number_%E2%80%93_Using_Python_math_trunc\"><\/span>Truncating a Complex Number &#8211; Using Python math trunc<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>A complex number consists of a real part and an imaginary part, both of which can be floating-point values. Truncation in the context of complex numbers refers to extracting the integer component of either the real or imaginary part while discarding the fractional portion.<\/p>\n<p>To illustrate, let&#8217;s consider a complex number, represented as 3.456 &#8211; 45.24j.<\/p>\n<p>First, we want to truncate the real part of the complex number. By applying the <code>math.trunc()<\/code> method to the real part, we can obtain its integer value.<\/p>\n<p>This method takes the initialized complex number as its argument, along with the <code>real<\/code> attribute, and returns the truncated integer value from the real part of the complex number.<\/p>\n<p>Let&#8217;s examine the <a href=\"https:\/\/tutorpython.com\/courses\/top-10-python-projects\/lesson\/introduction-to-python\/\" target=\"_blank\" rel=\"noopener\">Python<\/a> code snippet below to see this in action:<\/p>\n<pre class=\"language-python line-numbers\"><code>import math\r\ncomplex_number = 3.456 - 45.24j\r\ntruncated_real_part = math.trunc(complex_number.real)\r\nprint(truncated_real_part)    # prints 3\r\n<\/code><\/pre>\n<p>We applied the <code>math.trunc()<\/code> to the real part of the complex number by accessing the <code>real<\/code> attribute.<\/p>\n<p>The method returns the truncated integer value of the real part, which is\u00a0<code>3<\/code> in this case.<\/p>\n<p>Additionally, if we need to do the same thing as previously done, but this time we want to truncate the imaginary part of a complex number. For that, we will need to access the <code>imag<\/code> attribute.<\/p>\n<p>The next code snippet is a slight modification to the previous code:<\/p>\n<pre class=\"language-python line-numbers\"><code>import math\r\ncomplex_number = 3.456 - 45.24j\r\ntruncated_real_part = math.trunc(complex_number.imag)\r\nprint(truncated_real_part)    # prints -45\r\n<\/code><\/pre>\n<p>In this code, we use <code>math.trunc()<\/code> on the complex number&#8217;s imaginary part, accessed through the <code>imag<\/code> attribute.<\/p>\n<p>The method returns the truncated integer value of the imaginary part, which when <a href=\"https:\/\/tutorpython.com\/tutorial\/basic-input-output-operations-in-python\" target=\"_blank\" rel=\"noopener\">printed<\/a>, displays <code>-45<\/code>.<\/p>\n<p>There may be situations where we want to truncate both parts simultaneously and obtain a new complex number with the truncated values.<\/p>\n<p>To achieve this, we can define a <a href=\"https:\/\/tutorpython.com\/tutorial\/functions-in-python\" target=\"_blank\" rel=\"noopener\">custom function<\/a> that takes a complex <a href=\"https:\/\/tutorpython.com\/tutorial\/basic-input-output-operations-in-python\" target=\"_blank\" rel=\"noopener\">number as input<\/a>, applies the <code>math.trunc()<\/code> method to its real and imaginary parts, and returns a new complex number with the truncated values.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Heres_an_example_of_how_we_can_define_such_a_function_in_Python\"><\/span>Here&#8217;s an example of how we can define such a function in Python:<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<pre class=\"language-python line-numbers\"><code>import math\r\n\r\ndef truncate_complex(complex_number):\r\n    truncated_real = math.trunc(complex_number.real)\r\n    truncated_imaginary = math.trunc(complex_number.imag)\r\n    return complex(truncated_real, truncated_imaginary)\r\n\r\n# Example usage\r\noriginal_complex = 3.456 - 45.24j\r\ntruncated_complex = truncate_complex(original_complex)\r\nprint(truncated_complex) # prints (3-45j)\r\n<\/code><\/pre>\n<p>In this code snippet, we define the function <code>truncate_complex<\/code> that takes a <code>complex_number<\/code> <a href=\"https:\/\/tutorpython.com\/tutorial\/function-arguments-in-python\" target=\"_blank\" rel=\"noopener\">parameter<\/a>. Within the function, we use the <code>math.trunc()<\/code><code> method to truncate the real and imaginary parts of the complex number separately. The truncated values are then used to create a new complex number using the <code>complex()<\/code> constructor, which is then returned.<\/code><\/p>\n<p>To demonstrate the usage of the function, we initialize a complex number <code>original_complex<\/code> with the value <code>3.456 - 45.24j<\/code>.<\/p>\n<p>We then call <code>truncate_complex()<\/code>, passing <code>original_complex<\/code> as the argument.<\/p>\n<p>What we got was the complex number <code>(3-45j)<\/code>. since both the real and imaginary parts have been truncated.<\/p>\n<p>By defining a custom function like <code>truncate_complex()<\/code>, we can conveniently truncate both the real and imaginary parts of a complex number simultaneously and obtain a new complex number with the truncated values.<\/p>\n<p>This provides flexibility and simplicity when working with complex numbers and performing truncation operations.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>math.trunc()<\/code> method in Python provides a convenient way to truncate the fractional or decimal part of floating-point numbers and extract their integer component.<\/p>\n<p>By discarding the decimal portion, this method helps us achieve precise integer values for various calculations and data manipulations.<\/p>\n<p>Whether dealing with positive float numbers, negative float numbers, or complex numbers, <code>math.trunc()<\/code> proves to be a valuable tool.<\/p>\n<p>In this article, we explored the syntax and usage of the <code>math.trunc()<\/code> method through practical examples. By leveraging this method, Python developers can handle floating-point numbers with precision and flexibility.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Known for its versatility and extensive library support, Python provides programmers with&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[28],"tags":[],"class_list":["post-8141","post","type-post","status-publish","format-standard","hentry","category-tutorial"],"acf":[],"_links":{"self":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/8141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/comments?post=8141"}],"version-history":[{"count":31,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/8141\/revisions"}],"predecessor-version":[{"id":8816,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/posts\/8141\/revisions\/8816"}],"wp:attachment":[{"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/media?parent=8141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/categories?post=8141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorpython.com\/wp-json\/wp\/v2\/tags?post=8141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}