473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
Feb 10 '06 #1
7 1382
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
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
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
Thanks Guys. I'll divide by 100.
"Kerry Moorman" <Ke**********@discussions.microsoft.com> 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
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" <Ke**********@discussions.microsoft.com> 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
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" <Ke**********@discussions.microsoft.com> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Avinash Dhoot | last post by:
Hi, I have a 5 row table with the following values. key value 1 5 2 10 3 15 4 15 5 5
0
by: michael | last post by:
I've got a 3-column layout using percentages to define width of each div-column. #left-col { width: 20%; background-color: green; float:left; } #center-col { width: 60%; background-color: blue;...
1
by: Megan | last post by:
quick summary: i'm having problems trying to group fields in a report in order to calculate percentages. to calculate percentages, i'm comparing the results from my grouped fields to the totals....
2
by: Dot net work | last post by:
Hello. Is it possible to build up a dynamic server side table, and specify the column widths as percentages? I've tried this: Set the table width to 100%, then: FirstColumn.Width = New...
26
by: Mike Barnard | last post by:
Hi all. I am playing with html and css. I don't (yet) have a working site, I'm just trying to build a working, basic template I can use for a couple of ideas I have. I recall reading a...
5
by: Chris H | last post by:
I am wanting to write a function for a poker league manager that will detrmine players finishing points in a tournament depending on the amount of players that played... In other words when the...
1
by: Bob Alston | last post by:
I need to produce a report like this Color: # % ------ --- --- White 10 20 Black 25 50 other 15 30 ---- ---- Total 50 100
9
by: AZKing | last post by:
Hi all, I would like to know how do you go about calculating percentages in Access. For example, in a form I have 3 combo boxes with drop down menus where a user can select "Yes" or "No" and a...
3
by: martin DH | last post by:
Access 2003 I have a table (TASKS) filled with data from an external source. The table lists several tasks for employees to complete and has a text field (STATUS) that identifies where in the...
0
by: marcopolo8 | last post by:
Wierd thing is happening during my xHTML development. When I set the width attribute of a text field in a form and use percentages, if the text field's value is extremely long, IE6 automatically...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.