How to get numbers after decimal point?
282,189
Solution 1
An easy approach for you:
number_dec = str(number-int(number))[1:]
Solution 2
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.
Solution 3
Use modf:
>>> import math
>>> frac, whole = math.modf(2.5)
>>> frac
0.5
>>> whole
2.0
Solution 4
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)
Solution 5
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.
Related videos on Youtube
Author by
Alex Gordon
Check out my YouTube channel with videos on Azure development.
Updated on May 06, 2022Comments
-
Alex Gordon about 2 years
How do I get the numbers after a decimal point?
For example, if I have
5.55
, how do i get.55
?-
ire_and_curses over 13 years
-
Gulzar almost 3 yearsI think you should change the accepted answer on this. I almost didn't scroll down to the x10 times voted answer, and this would have bitten me later.
-
Abimael Domínguez over 2 yearsI think this is a simple approach:
float( '0.' + str(5.55).split('.')[1] )
>>> 0.55. But if someone thinks different, please let me know.
-
-
ire_and_curses over 13 yearsIf you're going to
import math
, why not just usemath.modf()
? -
Admin over 13 years@ire_and_curses: I am providing an alternative solution to the problem.
-
kindall over 13 yearsFor the benefit of the original question-asker: floats must be converted to a strings before they can be converted to Decimals. So if you have
n = 5.55
, n is a float, and you should doDecimal(str(n)) % 1
to get the fractional part. (This isn't necessary if you have an integer, but it doesn't hurt.) -
kindall over 13 yearsThis is OK for garden-variety numbers, but doesn't work so well for numbers large (or small) enough to require scientific notation.
-
bereal about 12 yearsAs for
modf
, it can screw the precision as well:math.modf(5.55)
will return (0.5499999999999998, 5.0). -
hokkuk over 11 yearsdoes anyone know which would be the faster operation, this method described above, or: float b = a - int(a) ? i suspect the later, but wanted to see if there was confirmation
-
intuited over 11 yearsExercise for the reader: make it work for numbers larger than or equal to 10
-
aherok about 11 years@intuited: It doesn't need to be decimal, it works also for float:
10.0/3 % 1
at least on my system -
QuantumKarl almost 11 yearsfloor does the same as casting to a int so could replace math.floor(t) with int(t)
-
Volatil3 about 10 yearsIf you want to omit decimal point then simply do this
number_dec = str(n-int(n))[2:]
-
Ryan Allen about 10 years
number_dec = str(number-int(number)).split('.')[1]
-
Kyle Heuton almost 10 years@intuited I was confused by your comment- this answer should work fine for numbers over 2 digits. You wont run into floating point issues until 2^14 (16384) for 2 decimal places, 2^13 (8192) for 3 decimal places, and so on.
-
Matthew Purdon over 9 yearsYou can use from_float instead of using a string. d = Decimal.from_float(1.2)
-
Stew over 9 yearsDownvote because this is an unnecessarily complicated approach. The answer I sought was the next answer,
5.55 % 1
, which is also a more generally useful answer--one can use the modulo division approach in multiple languages, whereas the above answer is Python-specific. -
macm about 9 years>>> frac 0.5499999999999998
-
T. Christiansen about 9 yearsNOTE: This solution returns a string including the dot (
.55
), which you may not expect due the questions title! -
Lambda Fairy about 8 years@QuantumKarl No, they are different.
math.floor(-1.1) == -2
butint(-1.1) == -1
. Though for this question usingint
is more correct. -
garg10may about 8 years@intuited still looking for that solution? any idea
-
Igor Soloydenko almost 8 yearsString based solutions are horrible if performance is important for your app.
-
D1X over 7 years@intuited How does one obtain the decimal part as an integer? I have tried to_integer() methods but the type is still Decimal.
-
coderforlife over 7 yearsOn a Raspberry Pi this method
x%1
was almost twice as fast as thex-int(x)
andmodf(x)[0]
methods (the timings were 980ns, 1.39us, and 1.47us averaged over 1000000 runs). My value forx
was always positive so I did not have to worry about that. -
Admin about 7 yearsAny explanation?
-
Priyank Mehta almost 7 years@Anthony Vallone answer is cleaner and correct approach
-
Cristian Ciupitu almost 7 years
str(5.55 - int(5.55))[1:]
returns.5499999999999998
instead of the.55
mentioned in the question. -
Enrico Borba over 6 yearsDefinitely the module operation is faster. It doesn't have to do any name lookups. Whereas calling the int function does a lookup on the global variables.
-
Meow over 6 yearsThis is nice because it return a non-fractional value, but will break on negative numbers
-
ZF007 about 6 yearsThe shortest and correct answer is posted here. The solution posted here is regardless of float/int subtracting "number - number" and result in null or zeros or 0.0!
-
user207421 almost 6 yearsUnfortunately it doesn't work most of the time. @Meow
-
Pedro Alves over 5 yearsThis is a real bad practice. Shouldn't be recommended as the accepted answer.
-
Lord Loh. over 5 yearsGood solution, but,
math.modf(2.53) = (0.5299999999999998, 2.0)
expected answer is 0.53 -
Mark Dickinson over 5 years
number = 5.55; "." in number
givesTypeError: argument of type 'float' is not iterable
. And what would you do ifnumber = 1e-5
? -
yosemite_k over 5 years@mark the question is How do I get the numbers after a decimal point? so user is expecting float in decimal notation (not scientific notation), I've added a block for scientific notation too
-
Mark Dickinson over 5 yearsA number is a number; it's only representations of a number that might be in scientific notation. So "float in decimal notation" doesn't make much sense here; by the time Python sees it, it's just a
float
; Python doesn't keep any knowledge of what format it was originally expressed in. Mynumber = 1e-5
example applies equally well tonumber = 0.00001
: thestr
representation of the number is in scientific notation. You'll want to deal withe+
as well ase-
, by the way. -
Rena over 5 years@LordLoh. that's because of floating point rounding, and will happen with any method.
-
Alex Gordon about 5 yearswhat does the operator // mean?
-
SirJ about 5 years@LordLoh. Try to use round(0.5299999999999998 , 2) to get 0.53 Another way is to use Decimal from decimal package. docs.python.org/2/library/decimal.html
-
John Y over 4 years@MatthewPurdon - If you use
Decimal.from_float(1.2)
(which can now be written asDecimal(1.2)
) you will have rounding issues, which this answer was trying to avoid. -
mckenzm about 4 yearsfancy not just having a frac()
-
Aditya Patnaik almost 4 yearswhat does this do for 0.000005?
-
Rasmus Damgaard Nielsen almost 4 yearsAnother reason this is bad: I'm european, and here str will give 5,55 (we use comma). So the code is also needlessly restricted to american users.
-
Kyoujin almost 4 yearsThis works perfectly, but could anyone explain why it works?
-
jer almost 4 yearsThe % operator is the modulo operator, and rather than explain it in great detail here, I'll link an article instead. jquery-az.com/python-modulo
-
run_the_race over 3 yearsCan test the speed with:
from timeit import timeit iterations = 1000000 result = timeit( 'n = 765.126357123; x = n -floor(n)', 'from math import floor', number=iterations ) print("Total time was:", result) print("Time per iterations was:", result/iterations)
-
M. Lautaro about 3 yearsBeware this breaks on negative numbers.
-
khanh about 3 yearsDownload because this is an unnecessarily complicated approach despite having simpler method.
-
Evgeni Sergeev almost 3 years@Rena More precisely: "... and will happen for any method where the result has to be given as an IEEE 754 double-precision float."
-
eric over 2 years@M.Lautaro it depends what behavior you want with negative numbers. E.g., for
-2.5
do you want the algorithm to return-0.5
or0.5
? OP didn't specify. For my use case, I am fine with the former, so this works great. -
Alejo about 2 yearsThis is the real solution
-
Alejo about 2 yearsfloat("0."+str(number).split('.')[1])