在python中使用小数点串(Using strings of decimals in python)
我试图使用一个十进制的字符串,但我得到一个不需要的“0”。 例如:
age = .01
print 'test%s'%(age)
print 'test' + str(age)
这两个都返回'test0.01',但我想'test.01'。 我知道有一个简单的解决方案。 有什么想法吗?
I'm trying to use a string of a decimal, but I am getting an unwanted "0.". For example:
age = .01
print 'test%s'%(age)
print 'test' + str(age)
These both return 'test0.01', but I want 'test.01'. I know there is a simple solution. Any thoughts?
原文:https://stackoverflow.com/questions/8606228
更新时间:2019-11-26 14:46
最满意答案
age = .01
print 'test%s' % str(age)[1:] if 0
age = .01
print 'test%s' % str(age)[1:] if 0
2011-12-22
相关问答
使用内置函数round() : In [23]: round(66.66666666666,4)
Out[23]: 66.6667
In [24]: round(1.29578293,6)
Out[24]: 1.295783
round()上的帮助: round(number [,ndigits]) - >浮点数 以十进制数字(默认为0位)将数字四舍五入为给定的精度。 这总是返回一个浮点数。 精度可能是负面的。 Use the built-in function round(): In [23
...
您要求输入精确度为零的有效数字。 如果您不允许存储任何数字,则所有数字都将为零。 请注意,精度是重要的小数位数,也就是说,不包括前导零。 0.5有一个有效数字, 1.5有两个。 另请注意, Decimal使用银行家舍入 。 You asked for zero significant digits of precision. If you don't allow any digits at all to be stored, all numbers will be zero. Note that
...
您需要指定您想要对浮点数进行操作。 例如: 3.0/4.0或只有3.0/4会给你浮点。 现在它只是执行整数操作。 编辑:你也可以使用float(3)/4 You need to specify that you want to operate on floating point numbers. For example: 3.0/4.0 or just 3.0/4 will give you floating point. Right now it's just performing intege
...
age = .01
print 'test%s' % str(age)[1:] if 0
age = .01
print 'test%s' % str(age)[1:] if 0
使用正则表达式的启发式方法: >>> import re
>>> s = """1.782-100.799
... -18.107-102.016
... -17.504104.059"""
>>> re.findall('-?\d{1,3}(?:\.\d{3})*', s)
['1.782', '-100.799', '-18.107', '-102.016', '-17.504', '104.059']
或者一行一行地获得配对: >>> [re.findall('-?\d{1,3}(?:\.
...
您正在两次插值: print("The volume of the cylinder is" + str(volume) + "{}cm\u00b3".format(volume))
只需一次就可以: print("The volume of the cylinder is {}cm\u00b3".format(volume))
关于.format()函数的.format()是你可以告诉它将你的数字格式化为一定数量的小数: print("The volume of the cylinder i
...
你的问题是令人困惑的,但是,请检查下面的代码和一些评论,如果它可以帮助你实现你想要的。 -- Populate numbers as varchar(4)
DECLARE @Numbers TABLE (Number varchar(4));
DECLARE @Number INT
SET @Number = 1
WHILE @Number <= 55
BEGIN
INSERT INTO @Numbers VALUES(@Number);
INSERT INTO @Numbe
...
浮点数的工作方式就像科学记数法。 1.0000000000000001e-21适合64位浮点数允许的有效位数/尾数的53位 。 加上1 ,比微小部分大许多数量级,导致细节被丢弃,并且精确地存储1 Floating point numbers work like scientific notation. 1.0000000000000001e-21 fits within the 53 bits of significand/mantissa a 64-bit float allows. Addin
...
toFixed返回一个字符串,并将字符串添加到一起。 当你用逗号替换句点时 ,你必须在数字加起来之后这样做,所以formatNum函数并不是真的可用,只需做到这一点 function calculatePublicTransportTotalClaim(){
var Bus_Weekly_Claim = Math.round( $('#Bus_Weekly_Claim').val() );
var Train_Weekly_Claim = Math.round( $('#Tr
...
你可以使用split >>> df['ENSGid'] = df['ENSGid'].apply(lambda x: x.split('.')[0])
'ENSG00000242268'
'ENSG00000270112'
...
You can use split >>> df['ENSGid'] = df['ENSGid'].apply(lambda x: x.split('.')[0])
'ENSG00000242268'
'ENSG00000270112'
...