kumar
kumar

Reputation: 2944

how to change decimal to % using .net

I have a variable Amount in my class.

This Amount value is coming from database as decimal something .0.000654345

While displaying in grid I need to show this as % value.

Upvotes: 5

Views: 380

Answers (6)

freak
freak

Reputation: 483

No need to multiply by 100. Use this

Console.WriteLine("Display percentage: {0:#.##%}", num);

Use # depending upon the precision.

Upvotes: 0

Michael Haren
Michael Haren

Reputation: 108246

Use the "P" format, which is hopefully more aware of globalization:

String.Format("{0:P4}", pos); // e.g. 1,000.0000%

You ought to be able to use this format string directly in your Gridview, too, as others have suggested. Since it does the 100x calc for you, though, it should actually work :). Perhaps like this:

<asp:BoundField DataField="Db" DataFormatString="{0:P4}" />

Upvotes: 10

Alex
Alex

Reputation: 35409

Multiply by 100 and parse the result as a string and format it appropriately...

String parsedNum = String.Format("{0}%", num * 100);

Upvotes: 3

LesterDove
LesterDove

Reputation: 3044

It sounds like you're implicitly asking a Gridview question as well, try:

<ItemTemplate>
   <asp:Label id="Label1" runat="server"
         Text='<%# Eval("Amount", "{0:0%}") %>' />
</ItemTemplate>

Upvotes: 0

MUG4N
MUG4N

Reputation: 19717

 Double yourValue = 0.00065;
 String percentage = (yourValue*100).ToString() + "%";

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500095

Well, that really depends on what it's meant to represent in its current form.

It may be as simple as multiplying by 100... but it could also be 10,000 depending on the exact meaning of "Amount".

What would you want displayed for 0.000654345?

Upvotes: 3

Related Questions