Open In App

Python – K length decimal Places

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, controlling the number of decimal places in your numeric values is essential for presenting results accurately and efficiently. This article explores techniques to manage K-length decimal places in Python. Given a Decimal Number, extend its digits to K length.

Input: num = 76.8, K = 5 
Output: 76.80000
Explanation: Length of decimal places is 5.
Input: num = 76.8, K = 6
Output: 76.800000
Explanation: Length of decimal places is 6.


Find K-length decimal Places using Format() 

Python’s format() function allows specifying decimal places for numeric values. This is the way in which this task can be done. In this, we harness multiple format compatibilities to perform the task of getting the appropriate length of decimal places.

Python3
num = 76.8

# printing original number
print("The original number is : " + str(num))

# initializing K
K = 7

# using format to solve this problem
res = "{{:.{}f}}".format(K).format(num)

print("The resultant number : " + str(res))

Output:

The original number is : 76.8
The resultant number : 76.8000000


Find K-length decimal Places using f-string

Python’s f-stringallows specifying decimal places for numeric values. First Define a function format_decimal_places that takes a number num and an integer K as inputs. Inside the function, use an f-string to format the number with the specified number of decimal places K. And it will Return the formatted number.

Python3
def format_decimal_places(num, K):
    formatted_num = f"{num:.{K}f}"
    return formatted_num

num = 76.8
K = 5
output = format_decimal_places(num, K)
print(output)

Output:

76.80000


Find K-length decimal Places using str(),index() and len() methods

In Python you can find K-length decimal places in a floating-point number using a combination of string manipulation methods such as str(), index(), and len().

Python3
num = 76.8

# printing original number
print("The original number is : " + str(num))

# initializing K
K = 7
num = str(num)
x = num[num.index(".")+1:]
y = "0"*(K-len(x))
res = num+y

print("The resultant number : " + str(res))

Output:

The original number is : 76.8
The resultant number : 76.8000000


Find K-length decimal Places using Round() Function

Python’s round() function to find K-length decimal places in a floating-point number involves rounding the number to K decimal places and then converting it back to a string to obtain the desired representation.

Python3
def find_k_decimal_places(num, k):
    rounded_num = round(num, k)
    rounded_str = str(rounded_num)
    decimal_index = rounded_str.index('.')
    return rounded_str[decimal_index + 1:]

# Example usage
value = 9.99999
k = 4
result = find_k_decimal_places(value, k)
print(result)

Output:

0

Find K-length decimal Places using the ‘Decimal’ class

  • Define the function decimal_class_decimal(num, k).
  • Convert the floating-point number num to a Decimal object using Decimal(str(num)).
  • Use the quantize() method to round the Decimal number to k decimal places with ROUND_HALF_UP rounding mode: .quantize(Decimal(f”1e-{k}”), rounding=ROUND_HALF_UP).
  • Return the quantized Decimal number.
  • Set initial values for num and k: num = 76.8 and k = 5.
  • Call the decimal_class_decimal function with the given values: result = decimal_class_decimal(num, k).
  • Print the result: print(result).
Python3
from decimal import Decimal, ROUND_HALF_UP

def decimal_class_decimal(num, k):
    # Convert the floating-point number 'num' to a Decimal object
    decimal_num = Decimal(str(num))
    
    # Quantize the Decimal number to 'k' decimal places using ROUND_HALF_UP rounding mode
    decimal_num = decimal_num.quantize(Decimal(f"1e-{k}"), rounding=ROUND_HALF_UP)
    
    # Return the quantized Decimal number
    return decimal_num

# Example usage:
# Set the initial values for 'num' and 'k'
num = 76.8
k = 5

# Call the 'decimal_class_decimal' function with the given values
result = decimal_class_decimal(num, k)

# Print the result
print(result)

Output
76.80000

Time Complexity: O(d + k)

Auxiliary Space Complexity: O(d + k)




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads