Velocity Reviews > Floating numbers and str

# Floating numbers and str

Jeremy Moles
Guest
Posts: n/a

 11-09-2005

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

Tuvas
Guest
Posts: n/a

 11-09-2005
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!

Jeffrey Schwab
Guest
Posts: n/a

 11-09-2005
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
>>>

Tuvas
Guest
Posts: n/a

 11-09-2005
Yep, I was thinking in C, not python. Thanks for the help!

Grant Edwards
Guest
Posts: n/a

 11-09-2005
On 2005-11-09, Tuvas <(E-Mail Removed)> 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!!

Fredrik Lundh
Guest
Posts: n/a

 11-09-2005
"Tuvas" <(E-Mail Removed)> 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>

Tuvas
Guest
Posts: n/a

 11-09-2005
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?

Grant Edwards
Guest
Posts: n/a

 11-09-2005
On 2005-11-09, Tuvas <(E-Mail Removed)> 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/li...s.html#l2h-211

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

Dan Bishop
Guest
Posts: n/a

 11-10-2005
Grant Edwards wrote:
> On 2005-11-09, Tuvas <(E-Mail Removed)> 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.

Grant Edwards
Guest
Posts: n/a

 11-10-2005
On 2005-11-10, Dan Bishop <(E-Mail Removed)> wrote:
> Grant Edwards wrote:
>> On 2005-11-09, Tuvas <(E-Mail Removed)> 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