用字符串和数字操作 [英] operation with strings and numbers
问题描述
你好的家伙
我是意大利男孩,我需要帮助做一个奇怪的事情用数字
我有一个来自这样的操作的号码:
a = float(" 010.123")
b = float(" 001.124")
p = a * b >
p ="%06.3f" %p
结果是_11,278其中_是一个空格
我必须在011278中更改此数字的格式。但是我不知道
做什么
你能帮我吗?
非常感谢
Hello guy
I''m italian boy, I need help for doing a strange thing with numbers
I have a number from operation like this:
a = float("010.123")
b = float("001.124")
p= a*b
p="%06.3f" % p
the resulted is "_11,278" where "_" is a space
I must to change the formatting of this number in "011278" but I don''t known
what to do
Could you help me please?
thanks a lot
推荐答案
Hellas写道:
你能帮我吗?
非常感谢
Could you help me please?
thanks a lot
我建议你读这个:
http://www.catb.org/~esr/faqs/smart-questions.html
然后阅读:
http:// www。 python.org/doc/2.3.4/lib/...q-strings.html
如果你仍然无法弄明白,请使用提示在第一份文件中
并再次询问。
-
Michael Hoffman
I suggest you read this:
http://www.catb.org/~esr/faqs/smart-questions.html
And then read this:
http://www.python.org/doc/2.3.4/lib/...q-strings.html
If you still can''t figure it out, use the hints in the first document
and ask again.
--
Michael Hoffman
Hellas写道:
结果是_11,278其中_是一个空间我必须在011278中更改此号码的格式。但我不知道该怎么做
the resulted is "_11,278" where "_" is a space
I must to change the formatting of this number in "011278" but I don''t known
what to do
所以你想要用''0'替换任何空格并摆脱
'',''(在这种情况下是十进制符号,因为你使用意大利语语言环境是
,而不是我在美国使用''。'' )
试试这个
So you want to replace the any space with a ''0'' and get rid
of the '','' (in this case the decimal sign because you''re
using an Italian locale, as compared to my US use of ''.'')
Try this
s =" 11,278
s.replace("," 0")
''011,278''s.replace(""," 0")。replace(", ","")
''011278''
另一个解决方案是使用''zfill''字符串方法。
s ="%6.3f" %p
s
''11 .378''s.replace("。","")。zfill(6)
''011378''
s = " 11,278"
s.replace(" ", "0") ''011,278'' s.replace(" ", "0").replace(",", "") ''011278''
Another solution is to use the ''zfill'' method of strings.
s = "%6.3f" % p
s ''11.378'' s.replace(".", "").zfill(6) ''011378''
Andrew
da *** @ dalkescientific.com
2004年10月15日星期五18:35:16 +0200,Hellas< he ******* @ hotmail.com>写道:
On Fri, 15 Oct 2004 18:35:16 +0200, Hellas <he*******@hotmail.com> wrote:
你好家伙
我是意大利男孩,我需要帮助做一些奇怪的事情用数字
我有一个来自这样的操作的数字:
a = float(" 010.123")
b = float(" 001.124")
p = a * b
p ="%06.3f" %p
结果是_11,278其中_是一个空间我必须在011278中更改此号码的格式。但我不知道该怎么做
你能帮我吗?
非常感谢
Hello guy
I''m italian boy, I need help for doing a strange thing with numbers
I have a number from operation like this:
a = float("010.123")
b = float("001.124")
p= a*b
p="%06.3f" % p
the resulted is "_11,278" where "_" is a space
I must to change the formatting of this number in "011278" but I don''t known
what to do
Could you help me please?
thanks a lot
有两个你需要做的事情:修复格式字符串,并且
在转换后处理字符串。假设你想要
do(1),有一个非常简单的错误需要解决。试试这个:
There are two things that you need to do: fix the format string, and
process the string after the conversion is made. Assuming you want to
do (1), there is a very simple mistake to fix. Try this:
"%07.3f" %p
''011.378''
而不是06,使用07和07。 - 记住你必须在浮点表示中计算。>
。完成后,删除
。从结果字符串中,用一个空的
字符串替换它:
s ="%07.3f" %p
s = s.replace("。","")
如果你想把所有事情都做成一行,那么只需这样做:
s =("%07.3f"%p).replace("。","")
另一种选择是在*格式化之前将浮点数转换为整数
*:
"%06d" %int(p * 1000)
"%07.3f" % p ''011.378''
Instead of "06", use "07" -- remember that you have to count the "."
in the floating point representation. After you''ve done that, remove
the "." from the resulting string, by replacing it with a empty
string:
s = "%07.3f" % p
s = s.replace(".","")
If you want to do everything into a single line, then just do it:
s = ("%07.3f" % p).replace(".","")
Another option is to convert the floating point number to a integer
*before* formatting:
"%06d" % int(p*1000)
''011378''
-
Carlos Ribeiro
Consultoria em Projetos
博客: http ://rascunhosrotos.blogspot.com
博客: http: //pythonnotes.blogspot.com
邮件: ca ****** **@gmail.com
邮件: ca ******* *@yahoo.com
''011378''
--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: ca********@gmail.com
mail: ca********@yahoo.com
这篇关于用字符串和数字操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!