Floating numbers and str

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tuvas

    #1

    Floating numbers and str

    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

    #2
    Re: Floating numbers and str

    Tuvas wrote:[color=blue]
    > 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![/color]

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

    Anyway, ITYW:

    "%.4g" % x

    E.g:
    [color=blue][color=green][color=darkred]
    >>> x=.13241414515
    >>> y="%.4g something here" % x
    >>> print y[/color][/color][/color]
    0.1324 something here[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Comment

    • Jeremy Moles

      #3
      Re: Floating numbers and str

      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:[color=blue]
      > 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!
      >[/color]

      Comment

      • Tuvas

        #4
        Re: Floating numbers and str

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

        Comment

        • Grant Edwards

          #5
          Re: Floating numbers and str

          On 2005-11-09, Tuvas <tuvas21@gmail. com> wrote:
          [color=blue]
          > I would like to limit a floating variable to 4 signifigant digits, when
          > running thorugh a str command.[/color]

          Sorry, that's not possible.
          [color=blue]
          > 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?[/color]

          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:
          [color=blue][color=green][color=darkred]
          >>> x=.13241414515
          >>> y = "%0.4g" % x
          >>> y[/color][/color][/color]
          '0.1324'

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

          Comment

          • Tuvas

            #6
            Re: Floating numbers and str

            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?

            Comment

            • Fredrik Lundh

              #7
              Re: Floating numbers and str

              "Tuvas" <tuvas21@gmail. com> wrote:
              [color=blue]
              >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![/color]

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



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



              here are some examples:
              [color=blue][color=green][color=darkred]
              >>> x=.13241414515
              >>> x[/color][/color][/color]
              0.13241414515[color=blue][color=green][color=darkred]
              >>> "%.4f" % x[/color][/color][/color]
              '0.1324'[color=blue][color=green][color=darkred]
              >>> "%.4g" % x[/color][/color][/color]
              '0.1324'
              [color=blue][color=green][color=darkred]
              >>> x=5.13241414515
              >>> "%.4f" % x[/color][/color][/color]
              '5.1324'[color=blue][color=green][color=darkred]
              >>> "%.4g" % x[/color][/color][/color]
              '5.132'

              </F>



              Comment

              • Grant Edwards

                #8
                Re: Floating numbers and str

                On 2005-11-09, Tuvas <tuvas21@gmail. com> wrote:[color=blue]
                > 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?[/color]



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

                Comment

                • Dan Bishop

                  #9
                  Re: Floating numbers and str

                  Grant Edwards wrote:[color=blue]
                  > On 2005-11-09, Tuvas <tuvas21@gmail. com> wrote:
                  >[color=green]
                  > > I would like to limit a floating variable to 4 signifigant digits, when
                  > > running thorugh a str command.[/color]
                  >
                  > Sorry, that's not possible.[/color]

                  Technically, it is.
                  [color=blue][color=green][color=darkred]
                  >>> class Float4(float):[/color][/color][/color]
                  .... def __str__(self):
                  .... return '%.4g' % self
                  ....[color=blue][color=green][color=darkred]
                  >>> x = Float4(1.234567 89)
                  >>> str(x)[/color][/color][/color]
                  '1.235'

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

                  Comment

                  • Grant Edwards

                    #10
                    Re: Floating numbers and str

                    On 2005-11-10, Dan Bishop <danb_83@yahoo. com> wrote:[color=blue]
                    > Grant Edwards wrote:[color=green]
                    >> On 2005-11-09, Tuvas <tuvas21@gmail. com> wrote:
                    >>[color=darkred]
                    >> > I would like to limit a floating variable to 4 signifigant digits, when
                    >> > running thorugh a str command.[/color]
                    >>
                    >> Sorry, that's not possible.[/color]
                    >
                    > Technically, it is.[/color]

                    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

                    Comment

                    • Jeffrey Schwab

                      #11
                      Re: Floating numbers and str

                      Tuvas wrote:[color=blue]
                      > 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?[/color]

                      s/%g/%f
                      [color=blue][color=green][color=darkred]
                      >>> print "%.4f stuff" % 1.3241414515[/color][/color][/color]
                      1.3241 stuff[color=blue][color=green][color=darkred]
                      >>> print "%.4f stuff" % 1.32042[/color][/color][/color]
                      1.3204 stuff[color=blue][color=green][color=darkred]
                      >>>[/color][/color][/color]

                      Comment

                      • Christian Stapfer

                        #12
                        Re: Floating numbers and str

                        "Tuvas" <tuvas21@gmail. com> wrote in message
                        news:1131565949 .678342.39890@g 44g2000cwa.goog legroups.com...[color=blue]
                        >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![/color]

                        from scipy import round

                        print round(0.1324141 4515,4)

                        Regards,
                        Christian



                        Comment

                        Working...