Advertisement
mayankjoin3

precision

Mar 13th, 2024
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.50 KB | None | 0 0
  1. def format_decimal(number, precision):
  2.     """
  3.    Format a decimal number to a specific precision.
  4.  
  5.    Args:
  6.        number (float): The decimal number to be formatted.
  7.        precision (int): The desired number of decimal places.
  8.  
  9.    Returns:
  10.        str: The formatted decimal number.
  11.    """
  12.     return f"{number:.{precision}f}"
  13.  
  14. # Example usage:
  15. number = 3.141592653589793
  16. precision = 3
  17. formatted_number = format_decimal(number, precision)
  18. print(formatted_number)  # Output: 3.142
  19.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement