w3resource

NumPy Input and Output: format_float_positional() function

numpy.format_float_positional() function

The format_float_positional() function is used to format a floating-point scalar as a decimal string in positional notation.

Provides control over rounding, trimming and padding. Uses and assumes IEEE unbiased rounding. Uses the “Dragon4” algorithm.

Syntax:

numpy.format_float_positional(x, precision=None, unique=True, fractional=True, trim='k',
sign=False, pad_left=None, pad_right=None)

Version: 1.15.0

Parameter:

Name Description Required /
Optional
x Value to format.
python float or numpy floating scalar
Required
precision Maximum number of digits to print. May be None if unique is True, but must be an integer if unique is False.
non-negative integer or None
Optional
unique If True, use a digit-generation strategy which gives the shortest representation which uniquely identifies the floating-point number from other values of the same type, by judicious rounding. If precision was omitted, print out all necessary digits, otherwise digit generation is cut off after precision digits and the remaining value is rounded. If False, digits are generated as if printing an infinite-precision value and stopping after precision digits, rounding the remaining value.
boolean
Optional
fractional If True, the cutoff of precision digits refers to the total number of digits after the decimal point, including leading zeros.
If False, precision refers to the total number of significant digits, before or after the decimal point, ignoring leading zeros.
boolean
Optional
trim Controls post-processing trimming of trailing digits, as follows:
  • 'k' : keep trailing zeros, keep decimal point (no trimming)
  • '.' : trim all trailing zeros, leave decimal point
  • '0' : trim all but the zero before the decimal point. Insert the zero if it is missing.
  • '-' : trim trailing zeros and any trailing decimal point

one of 'k', '.', '0', '-'
Optional
sign Whether to show the sign for positive values.
boolean
Optional
pad_left Pad the left side of the string with whitespace until at least that many characters are to the left of the decimal point.
non-negative integer
Optional
pad_right Pad the right side of the string with whitespace until at least that many characters are to the right of the decimal point.
non-negative integer
Optional

Returns: rep : string

The string representation of the floating point value.

NumPy.format_float_positional() method Example-1:

>>> import numpy as np
>>> np.format_float_positional(np.float32(np.pi))

Output:

'3.1415927'

NumPy.format_float_positional() method Example-2:

>>> import numpy as np
>>> np.format_float_positional(np.float16(np.pi))

Output:

'3.14'

NumPy.format_float_positional() method Example-3:

>>> import numpy as np
>>> np.format_float_positional(np.float16(0.3))

Output:

'0.3'

NumPy.format_float_positional() method Example-4:

>>> import numpy as np
>>> np.format_float_positional(np.float16(0.3), unique=False, precision=8)

Output:

'0.30004883'

Python - NumPy Code Editor:

Previous: array_str() function
Next: format_float_scientific() function



Share this Tutorial / Exercise on : Facebook and Twitter