Knowledgebase

Working With Numbers Print

  • python, woocommerce, troubleshooting, guide, howto, solution, zillionkinghost, hosting
  • 0

Arithmetic and precision.

THE TWO MAIN TYPES

Whole numbers, and numbers with decimal points.

WHAT TO KNOW ABOUT DIVISION

Python offers ordinary division, which produces a decimal, and floor division, which discards the remainder.

Choose deliberately.

THE PRECISION PROBLEM

Decimal numbers are stored in a way that cannot represent some values exactly.

WHAT THAT CAUSES

Small unexpected differences in arithmetic.

WHY IT MATTERS

For money, that is unacceptable.

WHAT TO USE FOR MONEY

A decimal type designed for exact arithmetic, or work in the smallest unit as whole numbers.

Never ordinary decimals for financial calculations.

THAT POINT

The single most important numeric consideration for business code.

ROUNDING

Be explicit about how and when you round.

Rounding at different points produces different totals.

CONVERTING TEXT TO NUMBERS

Explicit, and it fails on unexpected input.

Handle that failure rather than assuming clean data.

WHAT TO CHECK

That values you treat as numbers actually are.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot