# python中如何输入小数,python - 如何获得小数点后的数字？

python - 如何获得小数点后的数字？

17个解决方案

5.55 % 1

0.550000000001

>>> import math

>>> frac, whole = math.modf(2.5)

>>> frac

0.5

>>> whole

2.0

a = 1.3927278749291

b = a - int(a)

b

>> 0.39272787492910011

import numpy

a = 1.3927278749291

b = a - numpy.fix(a)

>>> from decimal import Decimal

>>> Decimal('4.20') % 1

Decimal('0.20')

number_dec = str(number-int(number))[1:]

5.55%1 = 0.54999999999999982

import math

orig = 5.55

whole = math.floor(orig) # whole = 5.0

frac = orig - whole # frac = 0.55

if "." in str(number): # quick check if it is decimal

number_dec = str(number).split(".")[1]

if 'e-' in str(number): # scientific notation

number_dec = format(float(number_dec), '.%df'%(len(number_dec.split(".")[1].split("e-")[0])+int(number_dec.split('e-')[1])))

>>> n=5.55

>>> if "." in str(n):

... print "."+str(n).split(".")[-1]

...

.55

>> 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

num = 45.7234

(whole, frac) = (int(num), int(str(num)[(len(str(int(num)))+1):]))

from decimal import *

x = Decimal('0.341343214124443151466')

str(x)[-2:] == '66' # True

y = 0.341343214124443151466

str(y)[-2:] == '66' # False

import math

x = 5.55

print((math.floor(x*100)%100))

In [4]: def split_float(x):

...: '''split float into parts before and after the decimal'''

...: before, after = str(x).split('.')

...: return int(before), (int(after)*10 if len(after)==1 else int(after))

...:

...:

In [5]: split_float(105.10)

Out[5]: (105, 10)

In [6]: split_float(105.01)

Out[6]: (105, 1)

In [7]: split_float(105.12)

Out[7]: (105, 12)

import math

x = 1245342664.6

print( (math.floor(x*1000)%1000) //100 )

a = 1.234

b = a - int(a)

length = len(str(a))

round(b, length-2)

print(b)

0.23399999999999999

round(b, length-2)

0.234

number = 123.456

temp = 1

while (number*temp)%10 != 0:

temp = temp *10

print temp

print number

temp = temp /10

number = number*temp

number_final = number%temp

print number_final

• 0
点赞
• 1
收藏
• 0
评论
09-18 1万+
10-17 114
09-07 622
11-16 1万+
07-12 5506
10-21 3万+
10-22 193
09-09 1363
06-28 600

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助