Q: Percentages

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

    #1

    Q: Percentages

    Hi

    Can anybody help me with a puzzling thing I've encountered with formating
    with percentages. If I have

    x = 123

    and then use format this with "P" (using the String.Format technique) I get

    12300%

    not the intended

    123%

    Can anybody tell me what I've misunderstood?

    Thanks in advance

    Geoff


  • Kerry Moorman

    #2
    RE: Q: Percentages

    Geoff,

    When formatting a value as a percent, a value in the form 1.23 is formatted
    as 123.00%.

    You could divide your value by 100 before formatting it to get the result
    you are looking for.

    Kerry Moorman

    "Geoff" wrote:
    [color=blue]
    > Hi
    >
    > Can anybody help me with a puzzling thing I've encountered with formating
    > with percentages. If I have
    >
    > x = 123
    >
    > and then use format this with "P" (using the String.Format technique) I get
    >
    > 12300%
    >
    > not the intended
    >
    > 123%
    >
    > Can anybody tell me what I've misunderstood?
    >
    > Thanks in advance
    >
    > Geoff
    >
    >
    >[/color]

    Comment

    • Cerebrus99

      #3
      Re: Percentages

      Well, the description for the "P" numeric format specification does say :
      "Displays number multiplied by 100 with a percent sign (%) appended to the
      right; always display two digits to the right of the decimal separator."

      So, I guess you should divide your value by 100 first, and then use the
      Format function.

      Dim x As Single = 1.23
      Console.WriteLi ne(Format(x, "Percent"))

      There is something to note, here:
      Somehow, whenever I try the abbreviated "p" or "P", it never works for me. I
      will get "p" as output. But when I enter the whole name "Percent", then it
      works. Wonder why this happens !

      Regards,
      Cerebrus.


      Comment

      • Kerry Moorman

        #4
        Re: Percentages

        Cerebrus99,

        It is confusing because the various options available for displaying a value
        as a percent are subtly different.

        For example, here are 3 different options, using ToString, String.Format and
        VB's Format function:

        Dim value As Single = 1.23

        MsgBox(value.To String("P"))
        MsgBox(String.F ormat("{0:P}", value))
        MsgBox(Format(v alue, "P"))

        Kerry Moorman


        "Cerebrus99 " wrote:
        [color=blue]
        > Well, the description for the "P" numeric format specification does say :
        > "Displays number multiplied by 100 with a percent sign (%) appended to the
        > right; always display two digits to the right of the decimal separator."
        >
        > So, I guess you should divide your value by 100 first, and then use the
        > Format function.
        >
        > Dim x As Single = 1.23
        > Console.WriteLi ne(Format(x, "Percent"))
        >
        > There is something to note, here:
        > Somehow, whenever I try the abbreviated "p" or "P", it never works for me. I
        > will get "p" as output. But when I enter the whole name "Percent", then it
        > works. Wonder why this happens !
        >
        > Regards,
        > Cerebrus.
        >
        >
        >[/color]

        Comment

        • Geoff

          #5
          Re: Percentages

          Thanks Guys. I'll divide by 100.


          "Kerry Moorman" <KerryMoorman@d iscussions.micr osoft.com> wrote in message
          news:691BC65A-F731-43E8-B025-6C0901198488@mi crosoft.com...[color=blue]
          > Cerebrus99,
          >
          > It is confusing because the various options available for displaying a
          > value
          > as a percent are subtly different.
          >
          > For example, here are 3 different options, using ToString, String.Format
          > and
          > VB's Format function:
          >
          > Dim value As Single = 1.23
          >
          > MsgBox(value.To String("P"))
          > MsgBox(String.F ormat("{0:P}", value))
          > MsgBox(Format(v alue, "P"))
          >
          > Kerry Moorman
          >
          >
          > "Cerebrus99 " wrote:
          >[color=green]
          >> Well, the description for the "P" numeric format specification does say :
          >> "Displays number multiplied by 100 with a percent sign (%) appended to
          >> the
          >> right; always display two digits to the right of the decimal separator."
          >>
          >> So, I guess you should divide your value by 100 first, and then use the
          >> Format function.
          >>
          >> Dim x As Single = 1.23
          >> Console.WriteLi ne(Format(x, "Percent"))
          >>
          >> There is something to note, here:
          >> Somehow, whenever I try the abbreviated "p" or "P", it never works for
          >> me. I
          >> will get "p" as output. But when I enter the whole name "Percent", then
          >> it
          >> works. Wonder why this happens !
          >>
          >> Regards,
          >> Cerebrus.
          >>
          >>
          >>[/color][/color]


          Comment

          • Cerebrus99

            #6
            Re: Percentages

            Hi Kerry,

            I understand that ToString(), String.Format() , and Format are subtly
            different. But are the Format specifications, "p", "P" and "Percent"
            different too ? Because, as I mentioned, Format(value, "P") doesn't work for
            me.

            Regards,

            Cerebrus.[color=blue]
            >
            > "Kerry Moorman" <KerryMoorman@d iscussions.micr osoft.com> wrote in message
            > news:691BC65A-F731-43E8-B025-6C0901198488@mi crosoft.com...[color=green]
            > > Cerebrus99,
            > >
            > > It is confusing because the various options available for displaying a
            > > value
            > > as a percent are subtly different.
            > >
            > > For example, here are 3 different options, using ToString, String.Format
            > > and
            > > VB's Format function:
            > >
            > > Dim value As Single = 1.23
            > >
            > > MsgBox(value.To String("P"))
            > > MsgBox(String.F ormat("{0:P}", value))
            > > MsgBox(Format(v alue, "P"))
            > >
            > > Kerry Moorman
            > >
            > >
            > > "Cerebrus99 " wrote:
            > >[color=darkred]
            > >> Well, the description for the "P" numeric format specification does say[/color][/color][/color]
            :[color=blue][color=green][color=darkred]
            > >> "Displays number multiplied by 100 with a percent sign (%) appended to
            > >> the
            > >> right; always display two digits to the right of the decimal[/color][/color][/color]
            separator."[color=blue][color=green][color=darkred]
            > >>
            > >> So, I guess you should divide your value by 100 first, and then use the
            > >> Format function.
            > >>
            > >> Dim x As Single = 1.23
            > >> Console.WriteLi ne(Format(x, "Percent"))
            > >>
            > >> There is something to note, here:
            > >> Somehow, whenever I try the abbreviated "p" or "P", it never works for
            > >> me. I
            > >> will get "p" as output. But when I enter the whole name "Percent", then
            > >> it
            > >> works. Wonder why this happens !
            > >>
            > >> Regards,
            > >> Cerebrus.
            > >>
            > >>
            > >>[/color][/color]
            >
            >[/color]


            Comment

            • Kerry Moorman

              #7
              Re: Percentages

              Cerebrus,

              For me, "P" and "p" work for ToSring, String.Format and VB's Format function.

              "Percent" does not work for ToString and String.Format, but it does work for
              VB's Format function.

              Example:

              Dim value As Single = 1.23

              MsgBox(value.To String("P"))
              MsgBox(String.F ormat("{0:P}", value))
              MsgBox(Format(v alue, "P"))

              MsgBox(value.To String("p"))
              MsgBox(String.F ormat("{0:p}", value))
              MsgBox(Format(v alue, "p"))

              MsgBox(value.To String("Percent ")) '<---- does not work
              MsgBox(String.F ormat("{0:Perce nt}", value)) '<---- does not work
              MsgBox(Format(v alue, "Percent"))

              Kerry Moorman


              "Cerebrus99 " wrote:
              [color=blue]
              > Hi Kerry,
              >
              > I understand that ToString(), String.Format() , and Format are subtly
              > different. But are the Format specifications, "p", "P" and "Percent"
              > different too ? Because, as I mentioned, Format(value, "P") doesn't work for
              > me.
              >
              > Regards,
              >
              > Cerebrus.[color=green]
              > >
              > > "Kerry Moorman" <KerryMoorman@d iscussions.micr osoft.com> wrote in message
              > > news:691BC65A-F731-43E8-B025-6C0901198488@mi crosoft.com...[color=darkred]
              > > > Cerebrus99,
              > > >
              > > > It is confusing because the various options available for displaying a
              > > > value
              > > > as a percent are subtly different.
              > > >
              > > > For example, here are 3 different options, using ToString, String.Format
              > > > and
              > > > VB's Format function:
              > > >
              > > > Dim value As Single = 1.23
              > > >
              > > > MsgBox(value.To String("P"))
              > > > MsgBox(String.F ormat("{0:P}", value))
              > > > MsgBox(Format(v alue, "P"))
              > > >
              > > > Kerry Moorman
              > > >
              > > >
              > > > "Cerebrus99 " wrote:
              > > >
              > > >> Well, the description for the "P" numeric format specification does say[/color][/color]
              > :[color=green][color=darkred]
              > > >> "Displays number multiplied by 100 with a percent sign (%) appended to
              > > >> the
              > > >> right; always display two digits to the right of the decimal[/color][/color]
              > separator."[color=green][color=darkred]
              > > >>
              > > >> So, I guess you should divide your value by 100 first, and then use the
              > > >> Format function.
              > > >>
              > > >> Dim x As Single = 1.23
              > > >> Console.WriteLi ne(Format(x, "Percent"))
              > > >>
              > > >> There is something to note, here:
              > > >> Somehow, whenever I try the abbreviated "p" or "P", it never works for
              > > >> me. I
              > > >> will get "p" as output. But when I enter the whole name "Percent", then
              > > >> it
              > > >> works. Wonder why this happens !
              > > >>
              > > >> Regards,
              > > >> Cerebrus.
              > > >>
              > > >>
              > > >>[/color]
              > >
              > >[/color]
              >
              >
              >[/color]

              Comment

              • Cerebrus99

                #8
                Re: Percentages

                Ah! I tried out all those combinations and our results match ! I must've
                been doing something wrong.

                Thanks a lot, Kerry.

                Regards,
                Cerebrus.



                Comment

                Working...