Convert a scientific notation to decimal number, and still keeping the data format as float64

Richard Damon Richard at Damon-Family.org
Fri Oct 18 10:53:07 EDT 2019


On 10/18/19 9:45 AM, doganadres at gmail.com wrote:
> On Friday, October 18, 2019 at 4:17:51 PM UTC+3, Richard Damon wrote:
>> On 10/18/19 9:03 AM, doganadres at gmail.com wrote:
>>> On Friday, October 18, 2019 at 2:21:34 PM UTC+3, Richard Damon wrote:
>>>> On 10/18/19 4:35 AM, doganadres at gmail.com wrote:
>>>>> Here is my question:
>>>>>
>>>>>
>>>>> I am using the numpy.std formula to calculate the standart 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 standart deviation result as a decimal number?
>>>>>
>>>>>
>>>>> Here is my code:
>>>>>
>>>>> stdev=numpy.std(dataset)
>>>>> print(stdev)
>>>>>     Result: 4.999999999999449e-05
>>>>>
>>>>>
>>>>> print(stdev.dtype)
>>>>>     Result: float64
>>>>>
>>>>>
>>>>> Solutions such as this:
>>>>>
>>>>> stdev=format(stdev, '.10f')
>>>>> converts the data into a string object! which I don't want.
>>>>>
>>>>>
>>>>> Expected result: I am willing to have a result as a decimal number in a float64 format.
>>>>>
>>>>> System: (Python 3.7.4 running on Win10)
>>>>>
>>>>>
>>>>> Regards, 
>>>> The number itself isn't in scientific notation or a fixed point number,
>>>> but is a floating point number that is expressed internally as an
>>>> integer times a power of 2 (that is the basic representation format for
>>>> floating point).
>>>>
>>>> Scientific notation vs fixed point notation is purely an OUTPUT
>>>> configuration, not a function on how the number is stored (so in one
>>>> sense IS more closely linked to a string than the float itself).
>>>>
>>>> -- 
>>>> Richard Damon
>>> Hello Richard,
>>>
>>> You seem so right. But what will we do with those strings with 'e' in it?
>>>
>>> There are ways to present those in a 'normal' way with formatting. However, the result of those formatting turns them into str object and which limits you to do any further numerical things with them.
>>>
>>> Another thing which I have observed, while doing some experimental thing is that:
>>>
>>>>>> 0.0
>>> 0.0
>>>
>>>>>> 0.1
>>> 0.1
>>>
>>>>>> 0.01
>>> 0.01
>>>
>>>>>> 0.001
>>> 0.001
>>>
>>>>>> 0.0001
>>> 0.0001
>>>
>>>>>> 0.00001
>>> 1e-05
>>>
>>> Again another thing with 1e-05! instead of 0.00001 , Is there anything I can do about it?
>> The 'e' is only part of the string representation presentation of the
>> value. By default, python decides to present small numbers in
>> exponential format.  If you don't like that, when you print the numbers
>> specify the format YOU want, rather than taking the default.
>>
>> Note, that print() by necessity converts an objects value to a string,
>> and this conversion for floats uses exponential notation by default for
>> small numbers. If you don't like it, then use an explicit format for the
>> conversion.
>>
>> An alternative might be to change from using a float64 to making it
>> something like a Decimal.
>>
>> -- 
>> Richard Damon
>
> By taking the default OUTPUT of a numpy formula, in my case standart deviation, I am using the advantage of saving the result into an excel file without any problems.(they come as numpy.float64) From there, The excel takes all the things as they are and some of my small numbers are shown with the 'e' on the excel sheet. Which I am trying to avoid.
>
> I don't need 100 numbers after the comma. What I need is a reasonable amount of decimal numbers to show that the number is small enough, also keeping them in float64, in my case to save them into excel file. One important thing to say is that, if I convert them into string and save them on excel they come with 'dot' instead of comma. And If try to translate the 'dot' manually into 'comma' the excel gives warning message to turn those inputs into numbers. Which I also avoid.
>
> If this is Python default, showing all numbers smaller than 0.0001 with 'e' and there is no way to have them in both a human readable and excel savable form.
>
> what sould I do?
>
> Should I consider using another programming language?
>
> If yes, then this language could be callable from within python and I might do things I can.

The problem is you are thinking of your floating point number as storing
something like 5e-5 or 0.00005, it doesn't. it store something more like
1.6384 x 2**15 (or some really big 50 some bit integer time a different
power of 2). As your words said, it SHOWS it with an e, but that isn't
what is in the number. The number is just a number represented as a float.

If you don't what to print them with the e, then format them when you
print to be the way you want.

As to the excel, you aren't being very clear about how you are
generating the excel file. If you are directly generating an .xls file,
then I believe you want to store the float64 value directly into it, and
their is no dot/comma as the decimal separator issue. More like it
seems, you are generating a 'CSV' file, which is a textual
representation, and if you are doing that then you need to be careful to
follow the rules for the CSV format you are using (and if you are using
the European format which uses comma as the decimal point,  that raises
other issues). Agian here, it isn't how the number is stored, but how it
is formatted for output.

print(x) is a quick and dirty method to display values, and isn't always
the right way. Sometimes you need to be more careful and explicitly
define the format to output the value.

Keep values internally in the right internal format, which here seems to
be float64. When outputting them (to user or file for another program)
be sure to handle any needed formatting if the defaults are suitable.

-- 
Richard Damon




More information about the Python-list mailing list