473,390 Members | 1,301 Online

operation with strings and numbers

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
thanks a lot
Jul 18 '05 #1
12 1414
Hellas wrote:
thanks a lot

http://www.catb.org/~esr/faqs/smart-questions.html

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
--
Michael Hoffman
Jul 18 '05 #2
Hellas wrote:
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

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'
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
Jul 18 '05 #3
On Fri, 15 Oct 2004 18:35:16 +0200, Hellas <he*******@hotmail.com> wrote:
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
thanks a lot

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'

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
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: ca********@gmail.com
mail: ca********@yahoo.com
Jul 18 '05 #4
>>>>> "Hellas" == Hellas <he*******@hotmail.com> writes:

Hellas> Hello guy

Hellas> I'm italian boy, I need help for doing a strange thing
Hellas> with numbers

Did you mean Italian SCHOOLboy, be any chance? Don't be afraid to say
so, because everyone can guess anyway.

Hellas> I have a number from operation like this:
Hellas> a = float("010.123")
Hellas> b = float("001.124")
Hellas> p= a*b
Hellas> p="%06.3f" % p
Hellas> the resulted is "_11,278" where "_" is a space

No, it isn't.

In [6]: "%06.3f" % p
Out[6]: '11.378'
In [7]: "%07.3f" % p
Out[7]: '011.378'

Hellas> I must to change the formatting of this number in "011278"
Hellas> but I don't known

In [8]: "lunttaus".replace("t","")
Out[8]: 'lunaus'

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #5
Ville Vainio wrote:

Did you mean Italian SCHOOLboy, be any chance? Don't be afraid to say
so, because everyone can guess anyway.

sorry what you mean?
Jul 18 '05 #6
>>>>> "liquid" == GMTaglia <li****@kuht.it> writes:

liquid> Ville Vainio wrote:

Did you mean Italian SCHOOLboy, be any chance? Don't be afraid to say
so, because everyone can guess anyway.

liquid> sorry what you mean?

Well, it was a school assignment, right?

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #7
I told "italian boy" only for my English that's orrible

Everyone says that in italian the english school is not good

Thanks for suppport
"Ville Vainio" <vi***@spammers.com> ha scritto nel messaggio
news:du*************@mozart.cc.tut.fi...
>> "Hellas" == Hellas <he*******@hotmail.com> writes:

Hellas> Hello guy

Hellas> I'm italian boy, I need help for doing a strange thing
Hellas> with numbers

Did you mean Italian SCHOOLboy, be any chance? Don't be afraid to say
so, because everyone can guess anyway.

Hellas> I have a number from operation like this:
Hellas> a = float("010.123")
Hellas> b = float("001.124")
Hellas> p= a*b
Hellas> p="%06.3f" % p
Hellas> the resulted is "_11,278" where "_" is a space

No, it isn't.

In [6]: "%06.3f" % p
Out[6]: '11.378'
In [7]: "%07.3f" % p
Out[7]: '011.378'

Hellas> I must to change the formatting of this number in "011278"
Hellas> but I don't known

In [8]: "lunttaus".replace("t","")
Out[8]: 'lunaus'

--
Ville Vainio http://tinyurl.com/2prnb

Jul 18 '05 #8
Ville thanks for the courage that you give to me
Jul 18 '05 #9
>I told "italian boy" only for my English that's orrible

Everyone says that in italian the english school is not good in Italy Thanks for suppport support

Excuse me
Jul 18 '05 #10
Hellas <he*******@hotmail.com> wrote:
I told "italian boy" only for my English that's orrible

Everyone says that in italian the english school is not good

May I suggest newsgroup it.comp.lang.python -- there, we talk about
Python in Italian.

Posso suggerire il gruppo it.comp.lang.python in cui parliamo di Python
in Italiano.
Alex
Jul 18 '05 #11
Sorry but It doesn't exist
Jul 18 '05 #12
Hellas <he*******@hotmail.com> wrote:
Sorry but It doesn't exist

You probably mean your ISP (flashnet) doesn't carry it. It's obviously
wrong for any Italian ISP to fail to carry the entire it.* official
hierarchy -- protest with them. Meanwhile, you can use Google Groups or
other non-NNTP hacks to read and write on it.comp.lang.python -- or
freely join "Individual Net", see <http://www.individual.net/> for
registration (I've done that because I travel so much that getting news
from any given ISP is impractical -- I previously used cheap
newsguy.com, but individual.net is completely free, and free's better
than cheap).
Alex
Jul 18 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

 2 by: dingbat | last post by: Losing my marbles here (some sleep would help!) Why are the following XPath comparisons both returning false ? ". The problem is that using the GNU compilers,... 0 by: jinu1996 | last post by: In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven... 0 by: Hystou | last post by: Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...