-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
164 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from decimal import Decimal, InvalidOperation | ||
|
||
|
||
def format_currency(value): # type: (float) -> str | None | ||
""" | ||
Formats a given float as Brazilian currency (R$). | ||
This function takes a float value and returns a formatted string representing | ||
the value as Brazilian currency, using the correct thousand and decimal separators. | ||
Args: | ||
value (float or Decimal): The numeric value to be formatted. | ||
Returns: | ||
str or None: A string formatted as Brazilian currency, or None if the input is invalid. | ||
Example: | ||
>>> format_currency(1234.56) | ||
"R$ 1.234,56" | ||
>>> format_currency(0) | ||
"R$ 0,00" | ||
>>> format_currency(-9876.54) | ||
"R$ -9.876,54" | ||
>>> format_currency(None) | ||
None | ||
>>> format_currency("not a number") | ||
None | ||
""" | ||
|
||
try: | ||
decimal_value = Decimal(value) | ||
formatted_value = ( | ||
f"R$ {decimal_value:,.2f}".replace(",", "_") | ||
.replace(".", ",") | ||
.replace("_", ".") | ||
) | ||
|
||
return formatted_value | ||
except InvalidOperation: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from decimal import Decimal | ||
from unittest import TestCase | ||
|
||
from brutils.currency import format_currency | ||
|
||
|
||
class TestFormatCurrency(TestCase): | ||
def test_when_value_is_a_decimal_value(self): | ||
assert format_currency(Decimal("123236.70")) == "R$ 123.236,70" | ||
|
||
def test_when_value_is_a_float_value(self): | ||
assert format_currency(123236.70) == "R$ 123.236,70" | ||
|
||
def test_when_value_is_negative(self): | ||
assert format_currency(-123236.70) == "R$ -123.236,70" | ||
|
||
def test_when_value_is_zero(self): | ||
print(format_currency(0)) | ||
assert format_currency(0) == "R$ 0,00" | ||
|
||
def test_value_decimal_replace_rounding(self): | ||
assert format_currency(-123236.7676) == "R$ -123.236,77" | ||
|
||
def test_when_value_is_not_a_valid_currency(self): | ||
assert format_currency("not a number") is None | ||
assert format_currency("09809,87") is None | ||
assert format_currency("897L") is None |