Floating numbers and str

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

  1. Jeremy Moles

    Jeremy Moles Guest

    I think you answered your own question. :)

    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
    #1
    1. Advertising

  2. Jeremy Moles

    Tuvas Guest

    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
    #2
    1. Advertising

  3. 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
    #3
  4. Jeremy Moles

    Tuvas Guest

    Yep, I was thinking in C, not python. Thanks for the help!
     
    Tuvas, Nov 9, 2005
    #4
  5. 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
    #5
  6. "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
    #6
  7. Jeremy Moles

    Tuvas Guest

    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
    #7
  8. 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
    #8
  9. Jeremy Moles

    Dan Bishop Guest

    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
    #9
  10. 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
    #10
  11. 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
    #11
  12. "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
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    2
    Views:
    493
    Thomas G. Marshall
    Aug 3, 2003
  2. maestro
    Replies:
    1
    Views:
    317
    Chris
    Aug 11, 2008
  3. newbie30

    *str++ and (*str)++

    newbie30, Aug 12, 2009, in forum: C Programming
    Replies:
    0
    Views:
    814
    newbie30
    Aug 12, 2009
  4. Ethan Furman
    Replies:
    4
    Views:
    262
    Roy Smith
    May 27, 2011
  5. Marco
    Replies:
    6
    Views:
    1,412
    Marco
    May 17, 2012
Loading...

Share This Page