# Floating numbers and str

Discussion in 'Python' started by Jeremy Moles, Nov 9, 2005.

1. ### Jeremy MolesGuest

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

On Wed, 2005-11-09 at 11:52 -0800, Tuvas wrote:
> 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!
>

Jeremy Moles, Nov 9, 2005

2. ### TuvasGuest

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!

Tuvas, Nov 9, 2005

3. ### Jeffrey SchwabGuest

Tuvas wrote:
> 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:

>>> x=.13241414515
>>> y="%.4g something here" % x
>>> print y

0.1324 something here
>>>

Jeffrey Schwab, Nov 9, 2005
4. ### TuvasGuest

Yep, I was thinking in C, not python. Thanks for the help!

Tuvas, Nov 9, 2005
5. ### Grant EdwardsGuest

On 2005-11-09, Tuvas <> wrote:

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

>>> x=.13241414515
>>> y = "%0.4g" % x
>>> y

'0.1324'

--
Grant Edwards grante Yow! A GRAM?? A BRAM... A
at GROOM... A BROOM... Oh,
visi.com Yeh!! Wash the ROOM!!

Grant Edwards, Nov 9, 2005
6. ### Fredrik LundhGuest

"Tuvas" <> wrote:

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

>>> x=.13241414515
>>> x

0.13241414515
>>> "%.4f" % x

'0.1324'
>>> "%.4g" % x

'0.1324'

>>> x=5.13241414515
>>> "%.4f" % x

'5.1324'
>>> "%.4g" % x

'5.132'

</F>

Fredrik Lundh, Nov 9, 2005
7. ### TuvasGuest

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?

Tuvas, Nov 9, 2005
8. ### Grant EdwardsGuest

On 2005-11-09, Tuvas <> wrote:
> 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?

http://www.python.org/doc/current/lib/typesseq-strings.html#l2h-211

--
Grant Edwards grante Yow! WHO sees a BEACH
at BUNNY sobbing on a SHAG
visi.com RUG?!

Grant Edwards, Nov 9, 2005
9. ### Dan BishopGuest

Grant Edwards wrote:
> On 2005-11-09, Tuvas <> wrote:
>
> > I would like to limit a floating variable to 4 signifigant digits, when
> > running thorugh a str command.

>
> Sorry, that's not possible.

Technically, it is.

>>> class Float4(float):

.... def __str__(self):
.... return '%.4g' % self
....
>>> x = Float4(1.23456789)
>>> str(x)

'1.235'

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

Dan Bishop, Nov 10, 2005
10. ### Grant EdwardsGuest

On 2005-11-10, Dan Bishop <> wrote:
> Grant Edwards wrote:
>> On 2005-11-09, Tuvas <> wrote:
>>
>> > I would like to limit a floating variable to 4 signifigant digits, when
>> > running thorugh a str command.

>>
>> Sorry, that's not possible.

>
> Technically, it is.

Ah well, I assumed that by "floating variable" he meant the
built-in "float" type.

--
Grant Edwards grante Yow! I always wanted a
at NOSE JOB!!
visi.com

Grant Edwards, Nov 10, 2005
11. ### Jeffrey SchwabGuest

Tuvas wrote:
> 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

>>> print "%.4f stuff" % 1.3241414515

1.3241 stuff
>>> print "%.4f stuff" % 1.32042

1.3204 stuff
>>>

Jeffrey Schwab, Nov 10, 2005
12. ### Christian StapferGuest

"Tuvas" <> wrote in message
news:...
>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

Christian Stapfer, Nov 10, 2005