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 floats to strings first.

Share:
282,189

Related videos on Youtube

Alex Gordon
Author by

Alex Gordon

Check out my YouTube channel with videos on Azure development.

Updated on May 06, 2022

Comments

  • Alex Gordon
    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
      ire_and_curses over 13 years
    • Gulzar
      Gulzar almost 3 years
      I 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
      Abimael Domínguez over 2 years
      I 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
    ire_and_curses over 13 years
    If you're going to import math, why not just use math.modf()?
  • Admin
    Admin over 13 years
    @ire_and_curses: I am providing an alternative solution to the problem.
  • kindall
    kindall over 13 years
    For 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 do Decimal(str(n)) % 1 to get the fractional part. (This isn't necessary if you have an integer, but it doesn't hurt.)
  • kindall
    kindall over 13 years
    This is OK for garden-variety numbers, but doesn't work so well for numbers large (or small) enough to require scientific notation.
  • bereal
    bereal about 12 years
    As for modf, it can screw the precision as well: math.modf(5.55) will return (0.5499999999999998, 5.0).
  • hokkuk
    hokkuk over 11 years
    does 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
    intuited over 11 years
    Exercise for the reader: make it work for numbers larger than or equal to 10
  • aherok
    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
    QuantumKarl almost 11 years
    floor does the same as casting to a int so could replace math.floor(t) with int(t)
  • Volatil3
    Volatil3 about 10 years
    If you want to omit decimal point then simply do this number_dec = str(n-int(n))[2:]
  • Ryan Allen
    Ryan Allen about 10 years
    number_dec = str(number-int(number)).split('.')[1]
  • Kyle Heuton
    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
    Matthew Purdon over 9 years
    You can use from_float instead of using a string. d = Decimal.from_float(1.2)
  • Stew
    Stew over 9 years
    Downvote 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
    macm about 9 years
    >>> frac 0.5499999999999998
  • T. Christiansen
    T. Christiansen about 9 years
    NOTE: This solution returns a string including the dot (.55), which you may not expect due the questions title!
  • Lambda Fairy
    Lambda Fairy about 8 years
    @QuantumKarl No, they are different. math.floor(-1.1) == -2 but int(-1.1) == -1. Though for this question using int is more correct.
  • garg10may
    garg10may about 8 years
    @intuited still looking for that solution? any idea
  • Igor Soloydenko
    Igor Soloydenko almost 8 years
    String based solutions are horrible if performance is important for your app.
  • D1X
    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
    coderforlife over 7 years
    On a Raspberry Pi this method x%1 was almost twice as fast as the x-int(x) and modf(x)[0] methods (the timings were 980ns, 1.39us, and 1.47us averaged over 1000000 runs). My value for x was always positive so I did not have to worry about that.
  • Admin
    Admin about 7 years
    Any explanation?
  • Priyank Mehta
    Priyank Mehta almost 7 years
    @Anthony Vallone answer is cleaner and correct approach
  • Cristian Ciupitu
    Cristian Ciupitu almost 7 years
    str(5.55 - int(5.55))[1:] returns .5499999999999998 instead of the .55 mentioned in the question.
  • Enrico Borba
    Enrico Borba over 6 years
    Definitely 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
    Meow over 6 years
    This is nice because it return a non-fractional value, but will break on negative numbers
  • ZF007
    ZF007 about 6 years
    The 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
    user207421 almost 6 years
    Unfortunately it doesn't work most of the time. @Meow
  • Pedro Alves
    Pedro Alves over 5 years
    This is a real bad practice. Shouldn't be recommended as the accepted answer.
  • Lord Loh.
    Lord Loh. over 5 years
    Good solution, but, math.modf(2.53) = (0.5299999999999998, 2.0) expected answer is 0.53
  • Mark Dickinson
    Mark Dickinson over 5 years
    number = 5.55; "." in number gives TypeError: argument of type 'float' is not iterable. And what would you do if number = 1e-5?
  • yosemite_k
    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
    Mark Dickinson over 5 years
    A 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. My number = 1e-5 example applies equally well to number = 0.00001: the str representation of the number is in scientific notation. You'll want to deal with e+ as well as e-, by the way.
  • Rena
    Rena over 5 years
    @LordLoh. that's because of floating point rounding, and will happen with any method.
  • Alex Gordon
    Alex Gordon about 5 years
    what does the operator // mean?
  • SirJ
    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
    John Y over 4 years
    @MatthewPurdon - If you use Decimal.from_float(1.2) (which can now be written as Decimal(1.2)) you will have rounding issues, which this answer was trying to avoid.
  • mckenzm
    mckenzm about 4 years
    fancy not just having a frac()
  • Aditya Patnaik
    Aditya Patnaik almost 4 years
    what does this do for 0.000005?
  • Rasmus Damgaard Nielsen
    Rasmus Damgaard Nielsen almost 4 years
    Another 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
    Kyoujin almost 4 years
    This works perfectly, but could anyone explain why it works?
  • jer
    jer almost 4 years
    The % 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
    run_the_race over 3 years
    Can 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
    M. Lautaro about 3 years
    Beware this breaks on negative numbers.
  • khanh
    khanh about 3 years
    Download because this is an unnecessarily complicated approach despite having simpler method.
  • Evgeni Sergeev
    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
    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 or 0.5? OP didn't specify. For my use case, I am fine with the former, so this works great.
  • Alejo
    Alejo about 2 years
    This is the real solution
  • Alejo
    Alejo about 2 years
    float("0."+str(number).split('.')[1])