0

I have a simple question.

I am using the numpy.std formula to calculate the standard deviation. However, the result comes as a number in scientific notation.

Therefore I am asking, How to convert a scientific notation to decimal number, and still keep the data format as float64 ?

Or is there any workaround to get the initial result as a decimal number?

Solutions such as this:

stdev = format(stdev, '.10f')

converts the data into a string object, which I don't want.

ere is my code:

stdev = numpy.std(dataset)
print(stdev)

Result: 4.999999999999449e-05

print(stdev.dtype)

Result: float64

Expected result

I willing to have a result as a decimal number in a float64 format.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
questioneer1
  • 5
  • 1
  • 3
  • Possible duplicate of [Convert scientific notation to decimal - python](https://stackoverflow.com/questions/16962512/convert-scientific-notation-to-decimal-python) – shuberman Oct 16 '19 at 07:00
  • Scientific notation is a display method, not the actual number. `0.00005` is displayed as "5e-05". You can use a print format to control the display, for example: `"%f"%0.00005`. Look at the docs for `np.set_printoptions` to see how you can control the display of numpy arrays. – hpaulj Oct 16 '19 at 07:15

2 Answers2

0

I suppose it is just a matter of representation since the data type, as you showed, is still float64.

If this is right, you can just print as a formatted string:

>>> x = 123456789e-15
>>> print(x)
1.23456789e-07

>>> print("{:.15f}".format(x))
0.000000123456789

The .15f specifies that you want a float format with 15 decimals.

From python 3.6 you can use the f-string format:

print(f"{x:.15f}")
0.000000123456789

Have a look at the string format documentation

MarcoP
  • 1,438
  • 10
  • 17
  • `print("{:.15f}".format(x))` this command may create an immediate output as a decimal number without string specifications. However when I assign this output to variable such as y, like : `y="{:.15f}".format(x)` the `y` object is not float anymore. It becomes a string. – questioneer1 Oct 16 '19 at 13:23
  • I am afraid I don't understand your question then. When you print a float it is converted to a string by python, so you are always seeing a string printed out. Depending on the number of decimal digits it may be printed in scientific notation (see https://stackoverflow.com/questions/24187684/python-change-repr-floating-digits). Nevertheless, the way a float is stored in memory does not depend on how you see it printed out by the print funcition. – MarcoP Oct 16 '19 at 13:54
0

You confuse representation and internal storage. By default, Python will show the value in the scientific form. You can force another form of representation - but you cannot affect the value itself.

Vanilla Python does not have a notion of float64 (it is not that great for exact calculations) - but numpy does

enter image description here

volcano
  • 3,578
  • 21
  • 28