[Python] Why does Decimal() appear in my Python environment but not in others?

Discussão em 'Python' iniciado por Stack, Março 7, 2025.

  1. Stack

    Stack Membro Participativo

    My codes below will take in price (e.g. $100.25) and convert it to decimal (100.25)

    from decimal import Decimal
    from price_parser import Price

    def priceToDecimal(amount, precision=2):
    parsed_price = Price.fromstring(str(amount)).amount_float
    parsed_price = Decimal(parsed_price)
    return parsed_price.quantize(Decimal(f"0.{'0' * precision}"))


    I use this function to parse a value and put it back into a the dict.

    In my python environment, the output dict:

    {
    "Amount": {
    "text": Decimal("249.26")
    }
    }


    In others' python environment, the output dict structure does not contain Decimal():

    {
    "Amount": {
    "text": "249.26"
    }
    }


    Both python environments are using the same python version: 3.10.12

    Any idea why it behaves such way?

    Edit: The price_parser library is from here https://github.com/scrapinghub/price-parser

    Continue reading...

Compartilhe esta Página