How do I get the numbers after a decimal point?
如何获得小数点后的数字?
For example, if I have 5.55
, how do i get .55
?
例如,如果我有5.55,我怎么得到.55?
15 个解决方案
#1
19
An easy approach for you:
一个简单的方法:
number_dec = str(number-int(number))[1:]
#2
122
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.
或者稍微偏离你期望的0.55。
#4
46
What about:
关于什么:
a = 1.3927278749291
b = a - int(a)
b
>> 0.39272787492910011
Or, using numpy:
或者,使用numpy:
import numpy
a = 1.3927278749291
b = a - numpy.fix(a)
#5
26
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.
作为注释中的所有注释,您必须首先将本机浮点数转换为字符串。
#6
5
Try Modulo:
试试Modulo:
5.55%1 = 0.54999999999999982
#7
4
import math
orig = 5.55
whole = math.floor(orig) # whole = 5.0
frac = orig - whole # frac = 0.55
#8
2
similar to the accepted answer, even easier approach using strings would be
类似于接受的答案,使用字符串更简单的方法
number_dec = str(number).split(".")[1]
#9
2
Example:
例:
import math
x = 5.55
print((math.floor(x*100)%100))
This is will give you two numbers after the decimal point, 55 from that example. If you need one number you reduce by 10 the above calculations or increase depending on how many numbers you want after the decimal.
这将给出小数点后面的两个数字,即该示例中的55。如果您需要一个数字,则将上述计算减少10或根据小数点后您想要的数量增加。
#10
1
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
#11
1
>>> n=5.55
>>> if "." in str(n):
... print "."+str(n).split(".")[-1]
...
.55
#12
1
Float numbers are not stored in decimal (base10) format. Have a read through the python documentation on this to satisfy yourself why. Therefore, to get a base10 representation from a float is not advisable.
浮点数不以十进制(base10)格式存储。阅读python文档,以满足自己的原因。因此,不建议从float中获取base10表示。
Now there are tools which allow storage of numeric data in decimal format. Below is an example using the Decimal
library.
现在有一些工具允许以十进制格式存储数字数据。下面是使用Decimal库的示例。
from decimal import *
x = Decimal('0.341343214124443151466')
str(x)[-2:] == '66' # True
y = 0.341343214124443151466
str(y)[-2:] == '66' # False
#13
0
This is a solution I tried:
这是我尝试过的解决方案:
num = 45.7234
(whole, frac) = (int(num), int(str(num)[(len(str(int(num)))+1):]))
#14
0
What about:
关于什么:
a = 1.234
b = a - int(a)
length = len(str(a))
round(b, length-2)
Output:print(b)
0.23399999999999999
round(b, length-2)
0.234
输出:打印(b)0.23399999999999999圆(b,长度-2)0.234
Since the round is sent to a the length of the string of decimals ('0.234'), we can just minus 2 to not count the '0.', and figure out the desired number of decimal points. This should work most times, unless you have lots of decimal places and the rounding error when calculating b interferes with the second parameter of round.
由于轮次被发送到小数串('0.234')的长度,我们可以只减2而不计算'0',并计算出所需的小数点数。这应该工作大多数时间,除非你有很多小数位,并且计算b时的舍入误差会干扰圆的第二个参数。
#15
-1
Another crazy solution is (without converting in a string):
另一个疯狂的解决方案是(不用字符串转换):
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
#1
19
An easy approach for you:
一个简单的方法:
number_dec = str(number-int(number))[1:]
#2
122
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.
或者稍微偏离你期望的0.55。
#3
#4
46
What about:
关于什么:
a = 1.3927278749291
b = a - int(a)
b
>> 0.39272787492910011
Or, using numpy:
或者,使用numpy:
import numpy
a = 1.3927278749291
b = a - numpy.fix(a)
#5
26
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.
作为注释中的所有注释,您必须首先将本机浮点数转换为字符串。
#6
5
Try Modulo:
试试Modulo:
5.55%1 = 0.54999999999999982
#7
4
import math
orig = 5.55
whole = math.floor(orig) # whole = 5.0
frac = orig - whole # frac = 0.55
#8
2
similar to the accepted answer, even easier approach using strings would be
类似于接受的答案,使用字符串更简单的方法
number_dec = str(number).split(".")[1]
#9
2
Example:
例:
import math
x = 5.55
print((math.floor(x*100)%100))
This is will give you two numbers after the decimal point, 55 from that example. If you need one number you reduce by 10 the above calculations or increase depending on how many numbers you want after the decimal.
这将给出小数点后面的两个数字,即该示例中的55。如果您需要一个数字,则将上述计算减少10或根据小数点后您想要的数量增加。
#10
1
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
#11
1
>>> n=5.55
>>> if "." in str(n):
... print "."+str(n).split(".")[-1]
...
.55
#12
1
Float numbers are not stored in decimal (base10) format. Have a read through the python documentation on this to satisfy yourself why. Therefore, to get a base10 representation from a float is not advisable.
浮点数不以十进制(base10)格式存储。阅读python文档,以满足自己的原因。因此,不建议从float中获取base10表示。
Now there are tools which allow storage of numeric data in decimal format. Below is an example using the Decimal
library.
现在有一些工具允许以十进制格式存储数字数据。下面是使用Decimal库的示例。
from decimal import *
x = Decimal('0.341343214124443151466')
str(x)[-2:] == '66' # True
y = 0.341343214124443151466
str(y)[-2:] == '66' # False
#13
0
This is a solution I tried:
这是我尝试过的解决方案:
num = 45.7234
(whole, frac) = (int(num), int(str(num)[(len(str(int(num)))+1):]))
#14
0
What about:
关于什么:
a = 1.234
b = a - int(a)
length = len(str(a))
round(b, length-2)
Output:print(b)
0.23399999999999999
round(b, length-2)
0.234
输出:打印(b)0.23399999999999999圆(b,长度-2)0.234
Since the round is sent to a the length of the string of decimals ('0.234'), we can just minus 2 to not count the '0.', and figure out the desired number of decimal points. This should work most times, unless you have lots of decimal places and the rounding error when calculating b interferes with the second parameter of round.
由于轮次被发送到小数串('0.234')的长度,我们可以只减2而不计算'0',并计算出所需的小数点数。这应该工作大多数时间,除非你有很多小数位,并且计算b时的舍入误差会干扰圆的第二个参数。
#15
-1
Another crazy solution is (without converting in a string):
另一个疯狂的解决方案是(不用字符串转换):
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