Scientific notation to decimal format in Python

David James Knight
26 Oct 2019

Python uses scientific notation to display very large and very small numbers. This function converts numbers formatted in scientific notation to ordinary decimal strings, with leading whitespace and trailing zeros removed. The default length is up to 10 decimals.


def decimal_str(x: float, decimals: int = 10) -> str:
    return format(x, f".{decimals}f").lstrip().rstrip('0')

Usage examples

x = 1 / 32768


>>> 3.0517578125e-05


>>> '0.0000305176'

Passing optional argument for decimal length:

print(decimal_str(x, 15))

>>> '0.000030517578125'
22 Jul 2019

Standard deviation in plain JavaScript

Standard deviation (“σ”, or sigma, in mathematical notation) is the amount of variation in a data set relative to its mean. A low standard deviation indicates that most of the values are grouped close to the mean, whereas a high standard deviation indicates that the values are spread out over a large range.

More >>
03 Jun 2019

Pipe functions in Python

A pipe function passes an object through a sequence of operations, with the output of each operation being the input of the next. Operations are executed in the order written, making pipes more readable than nested functions (which are executed from the inside out).

More >>
12 Sep 2018

Safely monkeypatching a .pipe() method in JavaScript

Use an ES6 Symbol to invoke a pipe function without the potential conflicts caused by monkeypatching a custom method onto the Object prototype.

More >>