Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

numpy
python
function
communitycreator

What is format_float_scientific() from NumPy in Python?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

Python’s format_float_scientific() function formats a floating-point scalar as a decimal string in scientific notation.

Syntax

format_float_scientific(x, precision=None, unique=True,   exp_digits=None)

Parameter value

The format_float_scientific() function takes the following parameter values:

  • x: This represents the floating-point value to be formatted.
  • precision: This represents the maximum number of digits to be printed. It takes a non-negative integer value and is optional.
  • unique: This takes boolean values (True or False). If True, the function uses a digit-generation strategy that gives the shortest representation. This uniquely identifies the floating-point number from other values of the same type. If False, the digits are generated as if printing an infinite-precision value and stopping after precision digits. The remaining value is rounded with unbiased rounding. This is optional.
  • exp_digits: This pads the exponents with zeros until it contains at least this many digits. If omitted, the exponent will be at least 2 digits. This is optional.

Return value

The format_float_scientific() function returns the string representation of a floating point value.

Code example

from numpy import float32, format_float_scientific
# creating a floating point value
x = float32(0.3)
# implementing the format_float_scientific() function
y = format_float_scientific(x, precision = 10, unique = False, exp_digits = 4)
print(y)
print(type(y))
Implementing the format_float_scientific() function

Code explanation

  • Line 1: We import float32 and format_float_scientific from the numpy module.
  • Line 4: We create a floating point integer of the float32 type using the float32() function. The output value is assigned to a variable x.
  • Line 7: We implement the format_float_scientific() function on the x variable. We use a precision value of 10, the unique value as False, and the exps_digits value of 4. The output is assigned to a variable y.
  • Line 9: We print the variable y, whose value is a string representation of the x value.
  • Line 10: We use the type() function to check for the data type of the y variable.

RELATED TAGS

numpy
python
function
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring