[Python] Python Decimal value insert into mariadb - acts strange

Discussão em 'Python' iniciado por Stack, Janeiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    The data type dayChangePercentage in mariadb is Decimal(11,3). I am inserting values from python and they are inconsistent and I am not able to figure out. Enclosed code.

    Code snippet:

    from decimal import *
    getcontext().prec = 5
    INSERT INTO currentQuotes (ticker, price, lastDiv, yield, prevClose, fiftyTwoWeekRange, dayChange, dayChangePercentage, beta, payoutRatio, nextEarningsDt, exDividendDt) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
    ('WEC', 99.03, 0, 0.0363, 98.39, '75.13 - 102.79', 0.64, Decimal('0.0065047'), 0.44, 80.32, datetime.date(2025, 2, 4), datetime.date(2025, 2, 14))


    The value in dayChangePercent is 0.0065

    Cope snippet:

    INSERT INTO currentQuotes (ticker, price, lastDiv, yield, prevClose, fiftyTwoWeekRange, dayChange, dayChangePercentage, beta, payoutRatio, nextEarningsDt, exDividendDt) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
    ('STM', 25.37, 0, 0.0146, 24.69, '23.66 - 49.05', 0.68, Decimal('0.027542'), 1.24, 12.35, datetime.date(2025, 1, 30), datetime.date(2025, 3, 25))


    The value in dayChangePercent for above in datbase is 0.03 - I was expecting 0.0275. Most of them the decimal is rounded to 2 decimal places and some of the values do honor the decimal precision.

    Am I missing something? I am confused

    Continue reading...

Compartilhe esta Página