Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added python number snippets #34

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions pysnippets/Numbers/Formatting_to_2_decimal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

# 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"

# 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()

40 changes: 40 additions & 0 deletions pysnippets/Numbers/padding_with_zeros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

# 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"

# 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()

40 changes: 40 additions & 0 deletions pysnippets/Numbers/percentage_formatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

# 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%"

# 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()