Skip to content

Commit

Permalink
feat: adiciona formatação monetária
Browse files Browse the repository at this point in the history
  • Loading branch information
morais90 committed Oct 21, 2024
1 parent 4af1cdc commit a2d8a7d
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 96 deletions.
39 changes: 33 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,18 @@ False
- [remove\_symbols\_pis](#remove_symbols_pis)
- [generate\_pis](#generate_pis)
- [Processo Jurídico](#processo-jurídico)
- [is\_valid\_legal\_process](#is_valid_legal_process)
- [is\_valid\_legal\_process](#is_valid_legal_process)
- [format\_legal\_process](#format_legal_process)
- [remove\_symbols\_legal\_process](#remove_symbols_legal_process)
- [generate\_legal\_process](#generate_legal_process)
- [Título Eleitoral](#titulo-eleitoral)
- [is_valid_voter_id](#is_valid_voter_id)
- [format_voter_id](#format_voter_id)
- [generate_voter_id](#generate_voter_id)
- [Titulo Eleitoral](#titulo-eleitoral)
- [is\_valid\_voter\_id](#is_valid_voter_id)
- [format\_voter\_id](#format_voter_id)
- [generate\_voter\_id](#generate_voter_id)
- [IBGE](#ibge)
- [convert_code_to_uf](#convert_code_to_uf)
- [convert\_code\_to\_uf](#convert_code_to_uf)
- [Monetário](#monetário)
- [format\_currency](#format_currency)

## CPF

Expand Down Expand Up @@ -1109,6 +1111,31 @@ Exemplo:
>>>
```

## Monetário

### format_currency

Formata um número seguindo o padrão monetário brasileiro. O número será formatado
adicionando o símbolo R$ como prefixo, vírgula como separador decimal, e ponto como
agrupador de milhar.

Argumentos:
* float ou Decimal: Um número com ou sem casas decimais.

Retorna:
* str ou None: O número formatado seguindo o padrão brasileiro.

Exemplo:

```python
>>> from brutils.currency import format_currency
>>> format_currency(1259.03)
'R$ 1.259,03'
>>> format_currency(0)
'R$ 0,00'
>>> format_currency("not a number")
None
```

# Novos Utilitários e Reportar Bugs

Expand Down
28 changes: 28 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ False
- [generate_voter_id](#generate_voter_id)
- [IBGE](#ibge)
- [convert_code_to_uf](#convert_code_to_uf)
- [Monetary](#monetary)
- [format_currency](#format_currency)

## CPF

Expand Down Expand Up @@ -1112,6 +1114,32 @@ Exemplo:
>>>
```

## Monetary

### format_currency

Formats a number following the Brazilian monetary standard. The number will be
formatted by adding the R$ symbol as a prefix, a comma as a decimal separator, and a
period as a thousands grouper.

Args:
* float or Decimal: A number with or without decimal places.

Returns:
* str or None: The number formatted following the Brazilian standard.

Example:

```python
>>> from brutils.currency import format_currency
>>> format_currency(1259.03)
'R$ 1.259,03'
>>> format_currency(0)
'R$ 0,00'
>>> format_currency("not a number")
None
```

# Feature Request and Bug Report

If you want to suggest new features or report bugs, simply create
Expand Down
126 changes: 36 additions & 90 deletions brutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,123 +4,67 @@
get_address_from_cep,
get_cep_information_from_address,
)
from brutils.cep import (
generate as generate_cep,
)
from brutils.cep import (
is_valid as is_valid_cep,
)
from brutils.cep import (
remove_symbols as remove_symbols_cep,
)
from brutils.cep import generate as generate_cep
from brutils.cep import is_valid as is_valid_cep
from brutils.cep import remove_symbols as remove_symbols_cep

# CNPJ Imports
from brutils.cnpj import (
format_cnpj,
)
from brutils.cnpj import (
generate as generate_cnpj,
)
from brutils.cnpj import (
is_valid as is_valid_cnpj,
)
from brutils.cnpj import (
remove_symbols as remove_symbols_cnpj,
)
from brutils.cnpj import format_cnpj
from brutils.cnpj import generate as generate_cnpj
from brutils.cnpj import is_valid as is_valid_cnpj
from brutils.cnpj import remove_symbols as remove_symbols_cnpj

# CPF Imports
from brutils.cpf import (
format_cpf,
)
from brutils.cpf import (
generate as generate_cpf,
)
from brutils.cpf import (
is_valid as is_valid_cpf,
)
from brutils.cpf import (
remove_symbols as remove_symbols_cpf,
)
from brutils.cpf import format_cpf
from brutils.cpf import generate as generate_cpf
from brutils.cpf import is_valid as is_valid_cpf
from brutils.cpf import remove_symbols as remove_symbols_cpf

# Currency
# Currency
from brutils.currency import format_currency

# Email Import
from brutils.email import is_valid as is_valid_email

# IBGE Imports
from brutils.ibge.uf import (
convert_code_to_uf,
)
from brutils.ibge.uf import convert_code_to_uf

# Legal Process Imports
from brutils.legal_process import (
format_legal_process,
)
from brutils.legal_process import (
generate as generate_legal_process,
)
from brutils.legal_process import (
is_valid as is_valid_legal_process,
)
from brutils.legal_process import (
remove_symbols as remove_symbols_legal_process,
)
from brutils.legal_process import format_legal_process
from brutils.legal_process import generate as generate_legal_process
from brutils.legal_process import is_valid as is_valid_legal_process
from brutils.legal_process import remove_symbols as remove_symbols_legal_process

# License Plate Imports
from brutils.license_plate import (
convert_to_mercosul as convert_license_plate_to_mercosul,
)
from brutils.license_plate import (
format_license_plate,
)
from brutils.license_plate import (
generate as generate_license_plate,
)
from brutils.license_plate import (
get_format as get_format_license_plate,
)
from brutils.license_plate import (
is_valid as is_valid_license_plate,
)
from brutils.license_plate import (
remove_symbols as remove_symbols_license_plate,
)
from brutils.license_plate import format_license_plate
from brutils.license_plate import generate as generate_license_plate
from brutils.license_plate import get_format as get_format_license_plate
from brutils.license_plate import is_valid as is_valid_license_plate
from brutils.license_plate import remove_symbols as remove_symbols_license_plate

# Phone Imports
from brutils.phone import (
format_phone,
remove_international_dialing_code,
remove_symbols_phone,
)
from brutils.phone import (
generate as generate_phone,
)
from brutils.phone import (
is_valid as is_valid_phone,
)
from brutils.phone import generate as generate_phone
from brutils.phone import is_valid as is_valid_phone

# PIS Imports
from brutils.pis import (
format_pis,
)
from brutils.pis import (
generate as generate_pis,
)
from brutils.pis import (
is_valid as is_valid_pis,
)
from brutils.pis import (
remove_symbols as remove_symbols_pis,
)
from brutils.pis import format_pis
from brutils.pis import generate as generate_pis
from brutils.pis import is_valid as is_valid_pis
from brutils.pis import remove_symbols as remove_symbols_pis

# Voter ID Imports
from brutils.voter_id import (
format_voter_id,
)
from brutils.voter_id import (
generate as generate_voter_id,
)
from brutils.voter_id import (
is_valid as is_valid_voter_id,
)
from brutils.voter_id import format_voter_id
from brutils.voter_id import generate as generate_voter_id
from brutils.voter_id import is_valid as is_valid_voter_id

# Defining __all__ to expose the public methods
__all__ = [
Expand Down Expand Up @@ -172,4 +116,6 @@
"is_valid_voter_id",
# IBGE
"convert_code_to_uf",
# Currency
"format_currency",
]
40 changes: 40 additions & 0 deletions brutils/currency.py
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
27 changes: 27 additions & 0 deletions tests/test_currency.py
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

0 comments on commit a2d8a7d

Please sign in to comment.