473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Could you help me please?
thanks a lot
Jul 18 '05 #1
12 1415
Hellas wrote:
Could you help me please?
thanks a lot


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
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
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:
"%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 ? <xsl:value-of select="string('2005-09-22T12:43:23') &gt;...
3
by: Mars | last post by:
I don't know whether the inputs are all numbers or all strings... if they are numbers, they are sorted as numbers, if they are strings, they are sorted as strings.... e.g. input : 9 12 2...
4
by: Sam Kong | last post by:
Hello! I wonder if there's a good way to make a function that calculates 2 big numbers (whole numbers) expressed as strings and return a result string with the value of a number? For example:...
12
by: Chadwick Boggs | last post by:
I need to perform modulo operations on extremely large numbers. The % operator is giving me number out of range errors and the mod(x, y) function simply seems to return the wrong results. Also,...
28
by: MLH | last post by:
The largest integer A97 can deal with is 2,147,483,647, as I understand it from HELP. I would be content to represent larger integers as strings. For example, "2147483648" would suit me fine. I...
52
by: Paddy | last post by:
I was browsing the Voidspace blog item on "Flattening Lists", and followed up on the use of sum to do the flattening. A solution was: I would not have thought of using sum in this way. When...
13
by: Otto Wyss | last post by:
I've this comparison with two numbers if (t.minimum.value t.maximum.value) ... yet it's always true except when minimum and maximum are identical. Why isn't it false when minimum is smaller...
0
by: Bart Kastermans | last post by:
|    def __str__ (self): I did some timing of operations involved. Doing this I found that the operation below to get a string representation for trees was in fact not quadratic. The final...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.