Skip to content

Instantly share code, notes, and snippets.

@victorfontes
Last active July 24, 2025 00:43
Show Gist options
  • Select an option

  • Save victorfontes/fb7f7d8e62e79319840bc933a11a7cd5 to your computer and use it in GitHub Desktop.

Select an option

Save victorfontes/fb7f7d8e62e79319840bc933a11a7cd5 to your computer and use it in GitHub Desktop.
finance stuff
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "039a2072",
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"from decimal import Decimal\n",
"\n",
"def parse_percentage(pct_str) -> float:\n",
" \"\"\"\n",
" Parses a percentage string into a float value.\n",
"\n",
" Handles formats like \"14,90%\", \"200 %\", and \"50%\", supporting both \".\" and \",\" as decimal separators.\n",
" Converts the percentage to a decimal (e.g., 14,90% becomes 0.149).\n",
" \"\"\"\n",
" if isinstance(pct_str, (float, Decimal)):\n",
" pct_str = str(pct_str)\n",
"\n",
" cleaned_string = pct_str.replace(\" \", \"\").replace(\"%\", \"\")\n",
"\n",
" # Replace comma with a dot for decimal conversion if a comma exists\n",
" if \",\" in cleaned_string and \".\" not in cleaned_string:\n",
" cleaned_string = cleaned_string.replace(\",\", \".\")\n",
" # If both exist, assume comma is the decimal separator and remove dots as thousand separators\n",
" elif \",\" in cleaned_string and \".\" in cleaned_string:\n",
" cleaned_string = cleaned_string.replace(\".\", \"\").replace(\",\", \".\")\n",
"\n",
" try:\n",
" return float(cleaned_string) / 100\n",
" except ValueError:\n",
" raise ValueError(f\"Could not parse '{pct_str}' as a percentage.\"))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a7d69e7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
pandas
fastcore
funcy
toolz
boltons
casefy
arrow
python-box
orjson
orjsonl
pydantic
glom
jsonpath-ng
import re
from decimal import Decimal
def parse_percent(pct_str: str) -> float:
"""
Parses a percentage string into a float value.
Handles formats like "14,90%", "200 %", and "50%", supporting both "." and "," as decimal separators.
Converts the percentage to a decimal (e.g., 14,90% becomes 0.149).
"""
if isinstance(pct_str, (float, Decimal)):
pct_str = str(pct_str)
cleaned_string = pct_str.replace(" ", "").replace("%", "")
# Replace comma with a dot for decimal conversion if a comma exists
if "," in cleaned_string and "." not in cleaned_string:
cleaned_string = cleaned_string.replace(",", ".")
# If both exist, assume comma is the decimal separator and remove dots as thousand separators
elif "," in cleaned_string and "." in cleaned_string:
cleaned_string = cleaned_string.replace(".", "").replace(",", ".")
try:
return float(cleaned_string) / 100
except ValueError:
raise ValueError(f"Could not parse '{pct_str}' as a percentage."))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment