python float to string in exponent without dot [duplicate] - python

This question already has answers here:
Use fixed exponent in scientific notation
(3 answers)
Display a decimal in scientific notation
(13 answers)
Closed 3 years ago.
Basically, given a float point number, say
f=0.25
I want
'25e-02'
I would only consider numbers with two decimals. How can I do this without multiplying 100??
If I simply do '%.1e'%0.25, I get '2.5e-01', which is not what I want. I wonder if there is any way that I can choose the number in the exponent part, namely the xx in e-xx.

Related

Python : Is a number too big? [duplicate]

This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 1 year ago.
I've learnt that python supports very large numbers with int itself.
But in this case :
print(int(12630717197566440579/10))
My answer is
1263071719756644096
and not
1263071719756644057
As it's supposed to be.
Can someone tell me why?

How to count decimals of an input? [duplicate]

This question already has answers here:
Determine precision and scale of particular number in Python
(11 answers)
Closed 2 years ago.
I want to know how many decimals an input has to then use that information to aproximate the result of an operation to the same number of decimals. For example:
example = float(input('n '))
Imagine the input is 1.99, how do I obtain the number of decimals (2) to then use it?
Here's a simple hack to it.
dec = len(str(example).split('.')[1])

Is there a Math function to strip off decimals from a float (in Python)? [duplicate]

This question already has answers here:
Splitting a number into the integer and decimal parts
(9 answers)
How to get numbers after decimal point?
(37 answers)
Closed 2 years ago.
If I have a float like 32.879, and I want to end up with just 0.879, I can think of a few ways to do it, like:
Convert to string, strip off everything before the ., convert back to float; OR
32.879 - int(32.879)
Both of those feel like hacks. Is there no pure math operation that can do this?
Sort of like using abs() instead of if x < 0: x = x*-1
I'm dealing with Python, but if anyone can tell me the math name for this operation, I can probably google the Python way to do it.

Decimal rounding in python [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Floating Point Numbers [duplicate]
(7 answers)
Closed 4 years ago.
I am trying to understand the decimal rounding behavior in python. What I find strange is the following:
1) Multiplying 70 by 11.46950 gives me 802.865
2) Multiplying 70 by 11.46750 gives me 802.7249999999999
Why is there extra precision in the second case and not the first case? I understand that internally, the decimal cannot be represented exactly. But that reason should also apply to the first case as well?
I am using python3.6.
Thanks

formatted printing of the exponent of a double in Python [duplicate]

This question already has answers here:
Force python to not output a float in standard form / scientific notation / exponential form [duplicate]
(6 answers)
Closed 4 years ago.
Are there any means by which one could tell Python 2.x/3.x to always use 3 digits for the exponent when printing a float (==IEEE754 double precision) in scientific format using the "E" format specifier (or another one)?
Unfortunately no, the width and precision of the format specifier only affect presentation of the mantissa. You will need to post-process the string if you want to affect the exponent.

Categories

Resources