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 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
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.
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.
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.
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.
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. > > >
Ah! I tried out all those combinations and our results match ! I must've
been doing something wrong.
Thanks a lot, Kerry.
Regards,
Cerebrus. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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;...
|
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....
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |