Floating Number format problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z

    #1

    Floating Number format problem


    How could I format the float number like this: (keep 2 digit
    precision)
    1.002 =1
    1.12 =1.12
    1.00 =1
    1.567 =1.57
    2324.012 =2324.01

    I can not find any Formatting Operations is able to meet my
    requirement.
    Any suggestion will be appreciated.

  • ici

    #2
    Re: Floating Number format problem

    On Jun 12, 10:10 am, <kelvin....@gma il.comwrote:
    How could I format the float number like this: (keep 2 digit
    precision)
    1.002 =1
    1.12 =1.12
    1.00 =1
    1.567 =1.57
    2324.012 =2324.01
    >
    I can not find any Formatting Operations is able to meet my
    requirement.
    Any suggestion will be appreciated.
    >>print "%.02f" % (2324.012)
    2324.01
    >>>


    Comment

    • Guest's Avatar

      #3
      Re: Floating Number format problem

      On 6 12 , 3 16 , ici <iltch...@gmail .comwrote:
      On Jun 12, 10:10 am, <kelvin....@gma il.comwrote:
      >
      >
      >
      How could I format the float number like this: (keep 2 digit
      precision)
      1.002 =1
      1.12 =1.12
      1.00 =1
      1.567 =1.57
      2324.012 =2324.01
      >
      I can not find any Formatting Operations is able to meet my
      requirement.
      Any suggestion will be appreciated.
      >print "%.02f" % (2324.012)
      2324.01
      >
      http://docs.python.org/tut/node9.htm...00000000000000
      thank you !
      But in these case:
      >>print '%.02f'%1.002
      1.00
      >>print '%.02f'%1.00
      1.00

      I just expect it to output "1" , but these way will output 1.00

      Comment

      • Gabriel Genellina

        #4
        Re: Floating Number format problem

        En Tue, 12 Jun 2007 05:46:25 -0300, <kelvin.you@gma il.comescribió:
        On 6 12 , 3 16 , ici <iltch...@gmail .comwrote:
        >On Jun 12, 10:10 am, <kelvin....@gma il.comwrote:
        >>
        How could I format the float number like this: (keep 2 digit
        precision)
        1.002 =1
        1.12 =1.12
        >>print "%.02f" % (2324.012)
        >2324.01
        >>
        But in these case:
        >
        >>>print '%.02f'%1.002
        1.00
        >>>print '%.02f'%1.00
        1.00
        >
        I just expect it to output "1" , but these way will output 1.00
        def my_formatter_om mitting_trailin g_zeroes(value) :
        result = '%.2f' % value
        if result[-3:]=='.00': result = result[:-3]
        return result

        for f in [1.0, 1.002, 1.12, 1.567, 2324.012]:
        print "%g -%s" % (f, my_formatter_om mitting_trailin g_zeroes(f))

        --
        Gabriel Genellina

        Comment

        • Gabriel Genellina

          #5
          Re: Floating Number format problem

          En Tue, 12 Jun 2007 05:46:25 -0300, <kelvin.you@gma il.comescribió:
          On 6 12 , 3 16 , ici <iltch...@gmail .comwrote:
          >On Jun 12, 10:10 am, <kelvin....@gma il.comwrote:
          >>
          How could I format the float number like this: (keep 2 digit
          precision)
          1.002 =1
          1.12 =1.12
          >>print "%.02f" % (2324.012)
          >2324.01
          >>
          But in these case:
          >
          >>>print '%.02f'%1.002
          1.00
          >>>print '%.02f'%1.00
          1.00
          >
          I just expect it to output "1" , but these way will output 1.00
          def my_formatter_om mitting_trailin g_zeroes(value) :
          result = '%.2f' % value
          if result[-3:]=='.00': result = result[:-3]
          return result

          for f in [1.0, 1.002, 1.12, 1.567, 2324.012]:
          print "%g -%s" % (f, my_formatter_om mitting_trailin g_zeroes(f))

          --
          Gabriel Genellina

          Comment

          • Marc Christiansen

            #6
            Re: Floating Number format problem

            Gabriel Genellina <gagsl-py2@yahoo.com.a rwrote:
            En Tue, 12 Jun 2007 05:46:25 -0300, <kelvin.you@gma il.comescribió :
            >
            >On 6 12 , 3 16 , ici <iltch...@gmail .comwrote:
            >>On Jun 12, 10:10 am, <kelvin....@gma il.comwrote:
            >>>
            >How could I format the float number like this: (keep 2 digit
            >precision)
            >1.002 =1
            >1.12 =1.12
            >>>print "%.02f" % (2324.012)
            >>2324.01
            >>>
            >But in these case:
            >>
            >>>>print '%.02f'%1.002
            >1.00
            >>>>print '%.02f'%1.00
            >1.00
            >>
            >I just expect it to output "1" , but these way will output 1.00
            >
            def my_formatter_om mitting_trailin g_zeroes(value) :
            result = '%.2f' % value
            if result[-3:]=='.00': result = result[:-3]
            return result
            >
            for f in [1.0, 1.002, 1.12, 1.567, 2324.012]:
            print "%g -%s" % (f, my_formatter_om mitting_trailin g_zeroes(f))
            Or:

            def my_other_format ter_ommitting_t railing_zeroes( value):
            result = '%.2f' % value
            return result.rstrip(' 0.')

            my_other_format ter_ommitting_t railing_zeroes( 1.102) == "1.1"

            Comment

            • Peter Otten

              #7
              Re: Floating Number format problem

              Marc Christiansen wrote:
              Gabriel Genellina <gagsl-py2@yahoo.com.a rwrote:
              >En Tue, 12 Jun 2007 05:46:25 -0300, <kelvin.you@gma il.comescribió:
              >>
              >>On 6 12 , 3 16 , ici <iltch...@gmail .comwrote:
              >>>On Jun 12, 10:10 am, <kelvin....@gma il.comwrote:
              >>>>
              >>How could I format the float number like this: (keep 2 digit
              >>precision)
              >>1.002 =1
              >>1.12 =1.12
              >>>>print "%.02f" % (2324.012)
              >>>2324.01
              Or:
              >
              def my_other_format ter_ommitting_t railing_zeroes( value):
              result = '%.2f' % value
              return result.rstrip(' 0.')
              Make that result.rstrip(" 0").rstrip(".") , or it may fail:
              >>("%.2f" % 100.0).rstrip(" .0")
              '1' # wrong
              >>("%.2f" % 100.0).rstrip(" 0").rstrip(" .")
              '100'

              Peter

              Comment

              • John Machin

                #8
                Re: Floating Number format problem

                On Jun 12, 8:04 pm, Marc Christiansen <use...@solar-empire.dewrote:
                Gabriel Genellina <gagsl-...@yahoo.com.a rwrote:
                En Tue, 12 Jun 2007 05:46:25 -0300, <kelvin....@gma il.comescribió:
                >
                On 6 12 , 3 16 , ici <iltch...@gmail .comwrote:
                >On Jun 12, 10:10 am, <kelvin....@gma il.comwrote:
                >
                How could I format the float number like this: (keep 2 digit
                precision)
                1.002 =1
                1.12 =1.12
                >>print "%.02f" % (2324.012)
                >2324.01
                >
                But in these case:
                >
                >>>print '%.02f'%1.002
                1.00
                >>>print '%.02f'%1.00
                1.00
                >
                I just expect it to output "1" , but these way will output 1.00
                >
                def my_formatter_om mitting_trailin g_zeroes(value) :
                result = '%.2f' % value
                if result[-3:]=='.00': result = result[:-3]
                return result
                >
                for f in [1.0, 1.002, 1.12, 1.567, 2324.012]:
                print "%g -%s" % (f, my_formatter_om mitting_trailin g_zeroes(f))
                >
                Or:
                >
                def my_other_format ter_ommitting_t railing_zeroes( value):
                result = '%.2f' % value
                return result.rstrip(' 0.')
                >
                my_other_format ter_ommitting_t railing_zeroes( 1.102) == "1.1"
                Marc, thanks for coming, but:
                >>my_other_form atter_ommitting _trailing_zeroe s(100.00)
                '1'
                >>>
                What does the OP want to happen with 1.2? I suspect he wants '1.2',
                not '1.20'

                Looks like a variation of Marc's idea will do the business:
                >>values = [100.0, 1.0, 1.2, 1.002, 1.12, 1.567, 2324.012]
                >>[('%.02f' % x).rstrip('0'). rstrip('.') for x in values]
                ['100', '1', '1.2', '1', '1.12', '1.57', '2324.01']

                Howzat?

                Comment

                • Marc Christiansen

                  #9
                  Re: Floating Number format problem

                  Peter Otten <__peter__@web. dewrote:
                  Marc Christiansen wrote:
                  >
                  >Gabriel Genellina <gagsl-py2@yahoo.com.a rwrote:
                  >>En Tue, 12 Jun 2007 05:46:25 -0300, <kelvin.you@gma il.comescribió :
                  >>>
                  >>>On 6 12 , 3 16 , ici <iltch...@gmail .comwrote:
                  >>>>On Jun 12, 10:10 am, <kelvin....@gma il.comwrote:
                  >>>>>
                  >>>How could I format the float number like this: (keep 2 digit
                  >>>precision)
                  >>>1.002 =1
                  >>>1.12 =1.12
                  >>>>>print "%.02f" % (2324.012)
                  >>>>2324.01
                  >Or:
                  >>
                  >def my_other_format ter_ommitting_t railing_zeroes( value):
                  > result = '%.2f' % value
                  > return result.rstrip(' 0.')
                  >
                  Make that result.rstrip(" 0").rstrip(".") , or it may fail:
                  >
                  >>>("%.2f" % 100.0).rstrip(" .0")
                  '1' # wrong
                  >>>("%.2f" % 100.0).rstrip(" 0").rstrip(" .")
                  '100'
                  Oops, didn't think of what happens if the value is a multiple of 10.
                  Thanks for correcting me. And thanks to John, who found it too.

                  Marc

                  Comment

                  Working...