round() and banker's rounding
Në shqip: Rrumbullakimi në Python
ShpjegimiSQ
Shumë fillestarë habiten: round(2.5) jep 2, jo 3! Python-i rrumbullakon gjysmat te numri çift më i afërt („banker's rounding“), që gabimet të mos grumbullohen. Për para, përdor decimal.
EnglishEN
Many beginners are surprised: round(2.5) gives 2, not 3! Python rounds halves to the nearest even number (“banker's rounding”) so errors don't pile up. For money, use decimal.
Si ta mendosh
Si një arkëtar i ndershëm që herë rrumbullakon lart e herë poshtë, që askush të mos humbasë gjithmonë.
Lexoje në anglisht
Like a fair cashier who sometimes rounds up and sometimes down, so nobody always loses.
Shembull kodipython
print(round(2.5), round(3.5)) # 2 4
print(round(4.567, 2)) # 4.57