operation with strings and numbers

H

Hellas

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
 
A

Andrew Dalke

Hellas said:
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

Another solution is to use the 'zfill' method of strings.

Andrew
(e-mail address removed)
 
C

Carlos Ribeiro

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

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:
'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:
'011378'


--
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: (e-mail address removed)
mail: (e-mail address removed)
 
V

Ville Vainio

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

Hellas

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 said:
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'
 
H

Hellas

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
 
A

Alex Martelli

Hellas said:
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
 
A

Alex Martelli

Hellas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top