Solved

String.Format as percentage based on decimal places

Posted on 2006-06-23
6
869 Views
Last Modified: 2012-06-21
I need to format some values in a gridview.  They all need to display as a percentage (0-100%), but only show the decimals if they exist:

i.e.
  99       -->   99%
  86.56  -->   86.56%
  75.2    -->   75.2%

Is there a way to do this automatically using string.format?  If not, what's the easiest way to achieve what I need??
0
Comment
Question by:Rouchie
  • 3
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
ibost earned 250 total points
ID: 16971515
Here's an example - maybe not the most elegant:

        Dim i As Double
        i = 98.1234

   ' Find location of the decimal place, if it exists
        Dim decimalLocation As Integer = IIf(i.ToString.IndexOf(".") >= 0, i.ToString.IndexOf("."), -1)

   ' Create the format string for %... such as P0, P1, P4, etc
        Dim precision As String = IIf(decimalLocation = -1, "P0", "P" & Len(i.ToString.Substring(decimalLocation + 1)))

   ' Divide i by 100 so 98.1234 becomes .981234 and then format as percentage making it 98.1234%
        Dim s As String = (i / 100).ToString(precision)

        Response.Write(s)


Output:
98.1234 %
0
 
LVL 10

Expert Comment

by:ibost
ID: 16971528
Actually you don't need the first IIF

This:
Dim decimalLocation As Integer = IIf(i.ToString.IndexOf(".") >= 0, i.ToString.IndexOf("."), -1)

Can just be:
Dim decimalLocation = i.ToString.IndexOf(".")
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 16974052
does this work?

               <asp:TemplateField >
                    <ItemTemplate>
                       <asp:Label ID="lb" runat="server" Text='<%# Eval("percent") %>'></asp:Label>%
                    </ItemTemplate>
                </asp:TemplateField>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Author Comment

by:Rouchie
ID: 16982301
ibost,
I've implemented your suggestion but it removes all decimal placing.  This isn't what I needed - I only wanted rid of the decimals if it was n.00% or n.50% for example.  Currently the value 98.27 is formatted on-screen as 99%.   Can what you've done be amended at all?
Here's my code:

Dim i As Double = e.Row.DataItem("SkillLevel")
Dim decimalLocation As Integer = i.ToString.IndexOf(".")
Dim precision As String = IIf(decimalLocation = -1, "P0", "P" & Len(i.ToString.Substring(decimalLocation + 1)))
Dim l As Label = CType(e.Row.FindControl("lbl_skillLevel"), Label)
l.Text = (i / 100).ToString(precision)

deanvanrooyen,
I believe that would just add a % sign to the end of the label, correct??  Not all entries actually have scores, so I only need formatting where a number exists.
0
 
LVL 10

Expert Comment

by:ibost
ID: 16985285
what is i equal to right after you assign it e.Row.DataItem("SkillLevel")  ?

Try to Response.Write(i.tostring()) and see what it's value is.  

The code I gave above works for me.  Assume the value is 99.234

the IndexOf the decimal is (decimalLocation) is 2.  It then sets a precision string equal to "P" + length of the substring beginning after the decimal.  In this case Len(i.ToString.SubString(decimalLocation + 1)) is equal to 3.  So precision should be P3.

Maybe try this?

Dim precision As String = IIf(decimalLocation = -1, "P0", "P" & Len(i.ToString.Substring(decimalLocation + 1)).ToString())
0
 
LVL 25

Author Comment

by:Rouchie
ID: 16985975
Sorry ibost, there was a slight error in my select statement which after tracing, showed that your first suggestion did work.

Thank you very much for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hidden Field Value 10 60
Re-position sub-options beneath the TAB 7 74
How to get the web content with different encodings 2 24
Authentication of Web Services 3 46
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now