Tag: decimal
4 รายการ
Blog

pythonmathfloat
Python math.fsum: บวกทศนิยมทั้งลิสต์ให้แม่น และรู้ว่าเมื่อไหร่ควรเลิกใช้ float กับเงิน
เทียบ total += x กับ sum() และ math.fsum บน Python 3.14 แบบรันจริง ดูว่าทำไมยอดรวมเพี้ยน อะไรแก้ได้ อะไรแก้ไม่ได้ พร้อม unittest
pythonfractionsdecimal
Python Fraction.from_number: รับเฉพาะตัวเลขโดยไม่เผลอ parse ข้อความ
ใช้ Fraction.from_number แยก numeric API จาก string parsing พร้อมทดสอบ int, float, Decimal และข้อควรระวังค่าทศนิยมฐานสอง
pythontomllibdecimal
Python tomllib + Decimal: อ่านเลขทศนิยมจาก config โดยไม่เสียเศษ
ใช้ parse_float ของ tomllib ส่งค่าทศนิยมเข้า Decimal ตั้งแต่ตอน parse พร้อม unittest ที่รันจริง
pythondecimalmoney
Python decimal.localcontext: ล็อกกฎปัดเศษเฉพาะงาน ไม่ทำ Context รั่วทั้งโปรแกรม
ใช้ decimal.localcontext จำกัด precision และ ROUND_HALF_UP ในขอบเขตสั้น พร้อม unittest ตรวจว่า global context ไม่ถูกเปลี่ยน