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])
Related
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?
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.
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.
This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 4 years ago.
I am looking for an expression that will truncate a float to at most a certain number of digits. I want to preserve a certain number of decimals, without having unnecessary trailing 0s.
So, this almost works as desired:
"{0:3.f"}.format(number)
For input 3.123000001:
"{0:.3f}".format(3.1230000001)
'3.123'
Great. But for input 3:
"{0:.3f}".format(3)
'3.000'
I figured out the answer while I was writing the question. Just add .rstrip('0') to the expression. So:
"{0:3.f}".format(number).rstrip('0')
This question already has answers here:
How to round to 2 decimals with Python? [duplicate]
(21 answers)
Closed 4 years ago.
How would I format my float digits so that there is always 7 digits no matter the direction?
The value could be any of the following but I always want 7 like below
0.0054233
1234567
123.1224
1.992923
The portion I have right now is
return "%.7f" % fd
How can I adjust that to get my desired output? or maybe link me to something similar I could try and read from..>?
Try if this can work for you:
n = 123.456789
decimals = 7 - len(str(int(n)))
print('{:.{prec}f}'.format(n, prec=decimals))
#=> 123.4568
The drawback is that it rounds up.
It depends on the context of the program, in my opinion... If you just want the numbers to the right to be 6 decimals, use:
"{:.6f}".format(whatevervar)
In other contexts maybe convert it to a string and use len() to evaluate the number of digits.
Hope this helps!
EDIT: Seeing your comments, I would recommend using a conditional to define what you are trying to do. When the number has no decimals (check this thread for how to do it: How to check if a float value is a whole number ) leave it as it is, when it has decimals, round it up with the code I posted above.