From b669001b93172ee21858cc4659e1b757346abe69 Mon Sep 17 00:00:00 2001 From: Ananya Gupta <145869907+ananyag309@users.noreply.github.com> Date: Sun, 6 Oct 2024 19:56:11 +0530 Subject: [PATCH 1/4] Add files via upload --- pysnippets/Numbers/Formatting_to_2_decimal.py | 23 ++++++++++++++++++ pysnippets/Numbers/padding_with_zeros.py | 24 +++++++++++++++++++ pysnippets/Numbers/percentage_formatting.py | 24 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 pysnippets/Numbers/Formatting_to_2_decimal.py create mode 100644 pysnippets/Numbers/padding_with_zeros.py create mode 100644 pysnippets/Numbers/percentage_formatting.py diff --git a/pysnippets/Numbers/Formatting_to_2_decimal.py b/pysnippets/Numbers/Formatting_to_2_decimal.py new file mode 100644 index 0000000..946bcd6 --- /dev/null +++ b/pysnippets/Numbers/Formatting_to_2_decimal.py @@ -0,0 +1,23 @@ + +# Formatting to 2 decimal.py + +def format_to_two_decimals(num): + """ + Format a number to two decimal places. + + Args: + num (float): The number to format. + + Returns: + str: The formatted number as a string with two decimal places. + + Example: + >>> format_to_two_decimals(1234.5678) + '1234.57' + """ + return f"{num:.2f}" + + +# Example usage +if __name__ == "__main__": + print(format_to_two_decimals(1234.5678)) # Output: "1234.57" diff --git a/pysnippets/Numbers/padding_with_zeros.py b/pysnippets/Numbers/padding_with_zeros.py new file mode 100644 index 0000000..6c29545 --- /dev/null +++ b/pysnippets/Numbers/padding_with_zeros.py @@ -0,0 +1,24 @@ + +# padding_with_zeros.py + +def pad_with_zeros(num, total_length): + """ + Pad a number with leading zeros to achieve a specified total length. + + Args: + num (int, float): The number to pad with zeros. + total_length (int): The total length of the final string. + + Returns: + str: The number padded with zeros. + + Example: + >>> pad_with_zeros(42, 5) + '00042' + """ + return f"{num:0{total_length}}" + + +# Example usage +if __name__ == "__main__": + print(pad_with_zeros(42, 5)) # Output: "00042" diff --git a/pysnippets/Numbers/percentage_formatting.py b/pysnippets/Numbers/percentage_formatting.py new file mode 100644 index 0000000..882f855 --- /dev/null +++ b/pysnippets/Numbers/percentage_formatting.py @@ -0,0 +1,24 @@ + +# percentage_formatting.py + +def format_percentage(value, decimal_places=2): + """ + Format a number as a percentage with a specified number of decimal places. + + Args: + value (float): The number to format as a percentage. + decimal_places (int): The number of decimal places (default is 2). + + Returns: + str: The formatted percentage as a string. + + Example: + >>> format_percentage(0.7567) + '75.67%' + """ + return f"{value * 100:.{decimal_places}f}%" + + +# Example usage +if __name__ == "__main__": + print(format_percentage(0.7567)) # Output: "75.67%" From 0ce7650c0fe304494f338407fd336745c03095a1 Mon Sep 17 00:00:00 2001 From: Ananya Gupta <145869907+ananyag309@users.noreply.github.com> Date: Sun, 6 Oct 2024 20:40:58 +0530 Subject: [PATCH 2/4] Test case of Formatting_to_2_decimal.py --- pysnippets/Numbers/Formatting_to_2_decimal.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pysnippets/Numbers/Formatting_to_2_decimal.py b/pysnippets/Numbers/Formatting_to_2_decimal.py index 946bcd6..b5eb254 100644 --- a/pysnippets/Numbers/Formatting_to_2_decimal.py +++ b/pysnippets/Numbers/Formatting_to_2_decimal.py @@ -21,3 +21,19 @@ def format_to_two_decimals(num): # Example usage if __name__ == "__main__": print(format_to_two_decimals(1234.5678)) # Output: "1234.57" + +# test_formatting_to_2_decimal.py + +from Formatting_to_2_decimal import format_to_2_decimal + +def test_format_to_2_decimal(): + assert format_to_2_decimal(3.14159) == '3.14' + assert format_to_2_decimal(2.5) == '2.50' + assert format_to_2_decimal(1.0) == '1.00' + assert format_to_2_decimal(0) == '0.00' + assert format_to_2_decimal(-2.345) == '-2.35' + print("All tests for format_to_2_decimal passed!") + +if __name__ == "__main__": + test_format_to_2_decimal() + From 8a4fcb6bf32bb6d05b04069af43a0fa3b36ca2c9 Mon Sep 17 00:00:00 2001 From: Ananya Gupta <145869907+ananyag309@users.noreply.github.com> Date: Sun, 6 Oct 2024 20:41:52 +0530 Subject: [PATCH 3/4] test case of padding_with_zeros.py --- pysnippets/Numbers/padding_with_zeros.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pysnippets/Numbers/padding_with_zeros.py b/pysnippets/Numbers/padding_with_zeros.py index 6c29545..3dd19eb 100644 --- a/pysnippets/Numbers/padding_with_zeros.py +++ b/pysnippets/Numbers/padding_with_zeros.py @@ -22,3 +22,19 @@ def pad_with_zeros(num, total_length): # Example usage if __name__ == "__main__": print(pad_with_zeros(42, 5)) # Output: "00042" + +# test_padding_with_zeros.py + +from Padding_with_zeros import pad_with_zeros + +def test_pad_with_zeros(): + assert pad_with_zeros(42, 5) == '00042' + assert pad_with_zeros(123, 6) == '000123' + assert pad_with_zeros(7, 2) == '07' + assert pad_with_zeros(0, 3) == '000' + assert pad_with_zeros(-5, 4) == '-05' + print("All tests for pad_with_zeros passed!") + +if __name__ == "__main__": + test_pad_with_zeros() + From 6aa3e1b428fc70df7901ad0968abe08dfaa1970f Mon Sep 17 00:00:00 2001 From: Ananya Gupta <145869907+ananyag309@users.noreply.github.com> Date: Sun, 6 Oct 2024 20:42:38 +0530 Subject: [PATCH 4/4] test case for percentage_formatting.py --- pysnippets/Numbers/percentage_formatting.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pysnippets/Numbers/percentage_formatting.py b/pysnippets/Numbers/percentage_formatting.py index 882f855..060a61f 100644 --- a/pysnippets/Numbers/percentage_formatting.py +++ b/pysnippets/Numbers/percentage_formatting.py @@ -22,3 +22,19 @@ def format_percentage(value, decimal_places=2): # Example usage if __name__ == "__main__": print(format_percentage(0.7567)) # Output: "75.67%" + +# test_percentage_formatting.py + +from Percentage_formatting import format_as_percentage + +def test_format_as_percentage(): + assert format_as_percentage(0.125) == '12.50%' + assert format_as_percentage(0) == '0.00%' + assert format_as_percentage(1) == '100.00%' + assert format_as_percentage(-0.75) == '-75.00%' + assert format_as_percentage(2.5) == '250.00%' + print("All tests for format_as_percentage passed!") + +if __name__ == "__main__": + test_format_as_percentage() +