Floating numbers and str

J

Jeremy Moles

I think you answered your own question. :)

x = 0.12345678
y = "%.4f something here" % x
 
T

Tuvas

I would like to limit a floating variable to 4 signifigant digits, when
running thorugh a str command. Ei,


x=.13241414515
y=str(x)+" something here"

But somehow limiting that to 4 sign. digits. I know that if you use the
print statement, you can do something like %.4d, but how can I do this
with converting the number to a string? Thanks!
 
J

Jeffrey Schwab

Tuvas said:
I would like to limit a floating variable to 4 signifigant digits, when
running thorugh a str command. Ei,


x=.13241414515
y=str(x)+" something here"

But somehow limiting that to 4 sign. digits. I know that if you use the
print statement, you can do something like %.4d, but how can I do this
with converting the number to a string? Thanks!

%d for a floating-point type? Is that right?

Anyway, ITYW:

"%.4g" % x

E.g:
 
G

Grant Edwards

I would like to limit a floating variable to 4 signifigant digits, when
running thorugh a str command.

Sorry, that's not possible.
x=.13241414515
y=str(x)+" something here"

But somehow limiting that to 4 sign. digits. I know that if
you use the print statement, you can do something like %.4d,
but how can I do this with converting the number to a string?

I don't understand what you mean about the print statement.

If using str() isn't really a requirement, you can use the
string formatting operator "%" like this:
'0.1324'
 
F

Fredrik Lundh

Tuvas said:
I would like to limit a floating variable to 4 signifigant digits, when
running thorugh a str command. Ei,

x=.13241414515
y=str(x)+" something here"

But somehow limiting that to 4 sign. digits. I know that if you use the
print statement, you can do something like %.4d, but how can I do this
with converting the number to a string? Thanks!

you mean "%.4g" ? the % operator is a string operator, and can be used
outside print:

text = "%.4g" % value

for more details, here's the first google hit for "python string formatting":

http://docs.python.org/lib/typesseq-strings.html

but that doesn't make too much sense if you don't know how things work
in C, which is explained here:

http://www.cplusplus.com/ref/cstdio/printf.html

here are some examples:
'0.1324'
'5.132'

</F>
 
T

Tuvas

Wait, one more question. If the number is something like:

1.32042

It is like
"1.32 stuff"

I would like it's size to remain constant. Any way around this?
 
D

Dan Bishop

Grant said:
Sorry, that's not possible.

Technically, it is.
.... def __str__(self):
.... return '%.4g' % self
....'1.235'

But, of course, I'd recommend just using '%.4g'%x directly.
 
J

Jeffrey Schwab

Tuvas said:
Wait, one more question. If the number is something like:

1.32042

It is like
"1.32 stuff"

I would like it's size to remain constant. Any way around this?
s/%g/%f
 
C

Christian Stapfer

Tuvas said:
I would like to limit a floating variable to 4 signifigant digits, when
running thorugh a str command. Ei,


x=.13241414515
y=str(x)+" something here"

But somehow limiting that to 4 sign. digits. I know that if you use the
print statement, you can do something like %.4d, but how can I do this
with converting the number to a string? Thanks!

from scipy import round

print round(0.13241414515,4)

Regards,
Christian
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top