454,165 Members | 1,502 Online
Need help? Post your question and get tips & solutions from a community of 454,165 IT Pros & Developers. It's quick & easy.

# Q: Percentages

 P: n/a 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 Feb 10 '06 #1
7 Replies

 P: n/a 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: 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 Feb 10 '06 #2

 P: n/a 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.WriteLine(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. Feb 10 '06 #3

 P: n/a 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.ToString("P")) MsgBox(String.Format("{0:P}", value)) MsgBox(Format(value, "P")) Kerry Moorman "Cerebrus99" wrote: 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.WriteLine(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. Feb 10 '06 #4

 P: n/a Thanks Guys. I'll divide by 100. "Kerry Moorman" wrote in message news:69**********************************@microsof t.com... 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.ToString("P")) MsgBox(String.Format("{0:P}", value)) MsgBox(Format(value, "P")) Kerry Moorman "Cerebrus99" wrote: 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.WriteLine(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. Feb 11 '06 #5

 P: n/a 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. "Kerry Moorman" wrote in message news:69**********************************@microsof t.com... 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.ToString("P")) MsgBox(String.Format("{0:P}", value)) MsgBox(Format(value, "P")) Kerry Moorman "Cerebrus99" wrote: 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.WriteLine(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. Feb 11 '06 #6

 P: n/a 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.ToString("P")) MsgBox(String.Format("{0:P}", value)) MsgBox(Format(value, "P")) MsgBox(value.ToString("p")) MsgBox(String.Format("{0:p}", value)) MsgBox(Format(value, "p")) MsgBox(value.ToString("Percent")) '<---- does not work MsgBox(String.Format("{0:Percent}", value)) '<---- does not work MsgBox(Format(value, "Percent")) Kerry Moorman "Cerebrus99" wrote: 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. "Kerry Moorman" wrote in message news:69**********************************@microsof t.com... 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.ToString("P")) MsgBox(String.Format("{0:P}", value)) MsgBox(Format(value, "P")) Kerry Moorman "Cerebrus99" wrote:> 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.WriteLine(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.>>> Feb 11 '06 #7

 P: n/a Ah! I tried out all those combinations and our results match ! I must've been doing something wrong. Thanks a lot, Kerry. Regards, Cerebrus. Feb 11 '06 #8

### This discussion thread is closed

Replies have been disabled for this discussion.