how do i get the numbers after a decimal point?
for example if i have 5.55
how do i get .55
?
how do i get the numbers after a decimal point?
for example if i have 5.55
how do i get .55
?
What about:
a = 1.3927278749291
b = a - int(a)
b
>> 0.39272787492910011
Or, using numpy:
import numpy
a = 1.3927278749291
b = a - numpy.fix(a)
5.55 % 1
Keep in mind this won't help you with floating point rounding problems. I.e., you may get:
0.550000000001
Or otherwise a little off the 0.55 you are expecting.
import math
orig = 5.55
whole = math.floor(orig) # whole = 5.0
frac = orig - whole # frac = 0.55
Use floor and subtract the result from the original number:
>> import math #gives you floor.
>> t = 5.55 #Give a variable 5.55
>> x = math.floor(t) #floor returns t rounded down to 5..
>> z = t - x #z = 5.55 - 5 = 0.55
Using the decimal
module from the standard library, you can retain the original precision and avoid floating point rounding issues:
>>> from decimal import Decimal
>>> Decimal('4.20') % 1
Decimal('0.20')
As kindall notes in the comments, you'll have to convert native float
s to strings first.
>>> n=5.55
>>> if "." in str(n):
... print "."+str(n).split(".")[-1]
...
.55