Skip to content

[Python] How To Take The Decimal Point Of A Value?

Last Updated on 2021-10-18 by Clay

I ran a personal website mainly to write my own study notes, so that I can review it by myself in the future. But a few weeks ago, I was asked by a friend who wanted to diligently and learn to write programs after work: How can I get the decimal point of a value?

That seems it’s a homework for the online course he took.

But yesterday I saw the following search keywords in my WordPress backend:

Python take a decimal

At the moment, I almost laughed out loud. Which website provided homework is this? It keeps appearing!

After thinking about it, I decided to write some simple examples.

I know that using Python’s built-in functions is the fastest way. But I am thinking: since it is the so-called homework, it should be for people to think about how to do it.

The problem to be solved in this article is also the 3 questions my friend asked me:

  • Take all decimals of the value
  • Take the specified number of decimal places of the value
  • Take the specified number of decimal places of the value (rounded)

Method of taking decimals

Before we start, I have to explain:

Strings are more accurate than floats. This is because floating numbers in computers are stored in binary, so the actual value may not be 0.1 as we see it. (In fact, it should be 0.xxxxxxxxxxx, but very close to 0.1)

The floating numbers we use now are in the format standardized by the famous IEEE 754 (for details, please refer to https://en.wikipedia.org/wiki/IEEE_754)

The following article uses string to handle decimals.


Question 1: Take all decimals of the value

This is the simplest case. Use string processing directly, take out all decimal parts, and add “0.” at the beginning.

# coding: utf-8


def main():
    value = 123.754640

    # Step 1: Convert to string
    ans = str(value)
    print("step 1:", ans)

    # Step 2: Split Integer and Decimal
    ans = ans.split('.')
    print("step 2:", ans)

    # Step 3: Get the Decimal
    ans = "0." + ans[1]
    print("step 3:", ans)


if __name__ == "__main__":
    main()


Output:

step 1: 123.75464
step 2: ['123', '75464']
step 3: 0.75464


Question 2: Take the specified number of decimal places of the value

This is a modification of the question 1. We also specify p as the number of decimal places.

# coding: utf-8


def main():
    value = 123.754640
    p = 3

    # Step 1: Convert to string
    ans = str(value)
    print("step 1:", ans)

    # Step 2: Split Integer and Decimal
    ans = ans.split('.')
    print("step 2:", ans)

    # Step 3: Get the decimal places
    ans = "0." + ans[1][:p]
    print("step 3:", ans)


if __name__ == "__main__":
    main()


Output:

step 1: 123.75464
step 2: ['123', '75464']
step 3: 0.754


Question 3: Take the specified number of decimal places of the value (rounded)

The question 3 is a deformation of the question 2. We need to take one more decimal place and judge whether the last digit larger than 5 or not.

It should be noted that in practice, you may encounter that there is no next digit in the decimal, but this example does not take it into consideration.

# coding: utf-8


def main():
    value = 123.754640
    p = 3

    # Step 1: Convert to string
    ans = str(value)
    print("step 1:", ans)

    # Step 2: Split Integer and Decimal
    ans = ans.split('.')
    print("step 2:", ans)

    # Step 3: Get the decimal places and the next place
    tail = ans[1][p]
    ans = ans[1][:p]
    print("step 3:", [ans, tail])

    # Step 4: Rounding
    if int(tail) >= 5:
        ans = str(int(ans)+1)

    ans = "0." + ans
    print("step 4:", ans)



if __name__ == "__main__":
    main()


Output:

step 1: 123.75464
step 2: ['123', '75464']
step 3: ['754', '6']
step 4: 0.755

References


Read More

Tags:

Leave a ReplyCancel reply

Exit mobile version