Getting the number of digits to the left of the decimal point is easy:

int(log10(x))+1

The number of digits to the right of the decimal point is trickier, because of the inherent inaccuracy of floating point values. I'll need a few more minutes to figure that one out.

Edit: Based on that principle, here's the complete code.

import math

def precision_and_scale(x):
    max_digits = 14
    int_part = int(abs(x))
    magnitude = 1 if int_part == 0 else int(math.log10(int_part)) + 1
    if magnitude >= max_digits:
        return (magnitude, 0)
    frac_part = abs(x) - int_part
    multiplier = 10 ** (max_digits - magnitude)
    frac_digits = multiplier + int(multiplier * frac_part + 0.5)
    while frac_digits % 10 == 0:
        frac_digits /= 10
    scale = int(math.log10(frac_digits))
    return (magnitude + scale, scale)
Answer from Mark Ransom on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › precision-handling-python
Precision Handling in Python - GeeksforGeeks
December 19, 2025 - Precision handling means controlling how many decimal places a number should have or how it should be rounded.
🌐
Python documentation
docs.python.org › 3 › tutorial › floatingpoint.html
15. Floating-Point Arithmetic: Issues and Limitations — Python 3.14.7 documentation
Why is that? 1/10 is not exactly representable as a binary fraction. Since at least 2000, almost all machines use IEEE 754 binary floating-point arithmetic, and almost all platforms map Python floats to IEEE 754 binary64 “double precision” values.
🌐
Reddit
reddit.com › r/learnpython › scientific precision in python?
r/learnpython on Reddit: Scientific Precision in Python?
April 18, 2022 -

When the matter is precision - like, really precise - floats are crap. Even double precision can be a gamble when working with scientific data - very small and very large numbers.

What are the best options when I need to work with numbers on the -15th and +20th orders of magnitude? (at the same time, mind you)

Is the decimal.py module precise enough for those sorts of calculations? If not, is it possible to get precise results with python or will I have to write some matlab modules to crunch my numbers?

🌐
Mpmath
mpmath.org
mpmath - Python library for arbitrary-precision floating-point arithmetic
mpmath internally uses Python's builtin long integers by default, but automatically switches to GMP for much faster high-precision arithmetic if gmpy2 is installed.
🌐
Prairielearn
docs.prairielearn.com › python-reference › prairielearn › to_precision
Numerical precision - PrairieLearn Docs
to_precision( value: Any, precision: int, notation: Notation = "auto", filler: str = "e", ) -> str
Top answer
1 of 16
2333

You are running into the old problem with floating point numbers that not all numbers can be represented exactly. The command line is just showing you the full floating point form from memory.

With floating point representation, your rounded version is the same number. Since computers are binary, they store floating point numbers as an integer and then divide it by a power of two so 13.95 will be represented in a similar fashion to 125650429603636838/(2**53).

Double precision numbers have 53 bits (16 digits) of precision and regular floats have 24 bits (8 digits) of precision. The floating point type in Python uses double precision to store the values.

For example,

>>> 125650429603636838/(2**53)
13.949999999999999

>>> 234042163/(2**24)
13.949999988079071

>>> a = 13.946
>>> print(a)
13.946
>>> print("%.2f" % a)
13.95
>>> round(a,2)
13.949999999999999
>>> print("%.2f" % round(a, 2))
13.95
>>> print("{:.2f}".format(a))
13.95
>>> print("{:.2f}".format(round(a, 2)))
13.95
>>> print("{:.15f}".format(round(a, 2)))
13.949999999999999

If you are after only two decimal places (to display a currency value, for example), then you have a couple of better choices:

  1. Use integers and store values in cents, not dollars and then divide by 100 to convert to dollars.
  2. Or use a fixed point number like decimal.
2 of 16
838

There are new format specifications, String Format Specification Mini-Language:

You can do the same as:

"{:.2f}".format(13.949999999999999)

Note 1: the above returns a string. In order to get as float, simply wrap with float(...):

float("{:.2f}".format(13.949999999999999))

Note 2: wrapping with float() doesn't change anything:

>>> x = 13.949999999999999999
>>> x
13.95
>>> g = float("{:.2f}".format(x))
>>> g
13.95
>>> x == g
True
>>> h = round(x, 2)
>>> h
13.95
>>> x == h
True
Find elsewhere
🌐
Medium
medium.com › @goldengrisha › understanding-floating-point-precision-issues-in-python-a-practical-guide-5e17b2f14057
Understanding Floating-Point Precision Issues in Python: A Practical Guide | by Gregory Kovalchuk | Medium
September 25, 2024 - In this article, we’ll explore what floating-point numbers are, why these precision errors happen, and how you can handle them effectively. At the core of most numerical computations are floating-point numbers. These are used to represent real numbers (numbers with decimal points) in a way that balances accuracy and performance. Floating-point numbers are stored in a format that includes: 1. A sign (positive or negative), 2. A mantissa (or fraction), 3. An exponent (which shifts the decimal point). In Python, floating-point numbers are represented using the IEEE 754 standard, which is the industry norm for binary floating-point arithmetic.
🌐
Mimo
mimo.org › glossary › python › float
Mimo: The coding platform you need to learn Web Development, Python, and more.
A float in Python is created by including a decimal point in a number. ... Dividing two numbers results in a float, even if both numbers are integers. ... # Generating a float by dividing two integers third = 1 / 3 # Results in a float, approximately 0.3333 · The return value of such operations is always a float, ensuring precision even when the inputs are whole numbers.
🌐
Python
docs.python.org › 3 › library › decimal.html
decimal — Decimal fixed-point and floating-point arithmetic
With two arguments, compute x**y. If x is negative then y must be integral. The result will be inexact unless y is integral and the result is finite and can be expressed exactly in ‘precision’ digits. The rounding mode of the context is used. Results are always correctly rounded in the Python version.
🌐
Real Python
realpython.com › how-to-python-f-string-format-float
How to Format Floats Within F-Strings in Python – Real Python
March 18, 2026 - To use Python’s format specifiers in a replacement field, you separate them from the expression with a colon (:). As you can see, your float has been rounded to two decimal places. You achieved this by adding the format specifier .2f into the replacement field. The 2 is the precision, while the lowercase f is an example of a presentation type.
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › functions › format.html
format — Python Reference (The Right Way) 0.1 documentation
The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with ‘f’ and ‘F’, or before and after the decimal point for a floating point value formatted with ‘g’ or ‘G’. For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content.
🌐
Python Engineer
python-engineer.com › posts › precision-handling
Precision Handling in Python | floor, ceil, round, trunc, format - Python Engineer
May 3, 2022 - Precision handling is a process of rounding off the values of floating-point numbers. Python has many built-in functions to handle the precision, like floor, ceil, round, trunc, and format.
🌐
scikit-learn
scikit-learn.org › stable › modules › generated › sklearn.metrics.precision_score.html
precision_score — scikit-learn 1.9.1 documentation
The precision is the ratio tp / (tp + fp) where tp is the number of true positives and fp the number of false positives.
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.14.7 documentation
There are three distinct numeric types: integers, floating-point numbers, and complex numbers. In addition, Booleans are a subtype of integers. Integers have unlimited precision.
🌐
Python
docs.python.org › 3 › library › time.html
time — Time access and conversions
Return the resolution (precision) of the specified clock clk_id.
🌐
LinkedIn
linkedin.com › pulse › navigating-nuances-numeric-precision-deep-dive-python-utkarsh-singh-ccemc
Navigating the Nuances of Numeric Precision: A Deep Dive into Python Floats and Decimals
December 7, 2023 - At the foundation of Python’s numeric system lies the float type, adhering to the IEEE 754 standard for floating-point arithmetic. This standard provides a standardized binary representation of real numbers. However, due to the inherent limitations of binary representation, rounding errors can occur, introducing imprecision in certain scenarios. In response to precision concerns associated with float, the decimal module introduces the Decimal type.
🌐
Wikitechy
wikitechy.com › tutorials › python › python-float-precision
python tutorial - Python Float Precision | Precision Handling in Python - By Microsoft Award MVP - learn python - python programming - Learn in 30sec | wikitechy
2. Using format() :- This is yet another way to format the string for setting precision. 3. Using round(x,n) :- This function takes 2 arguments, number and the number till which we want decimal part rounded. ... # Python code to demonstrate precision # and round() # initializing value a = 3.4536 # using "%" to print value till 2 decimal places print ("The value of number till 2 decimal place(using %) is : ",end="") print ('%.2f'%a) # using format() to print value till 2 decimal places print ("The value of number till 2 decimal place(using format()) is : ",end="") print ("{0:.2f}".format(a)) # using round() to print value till 2 decimal places print ("The value of number till 2 decimal place(using round()) is : ",end="") print (round(a,2))
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-decimal-division-round-precision
Python decimal - division, round, precision | DigitalOcean
Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.