How to format data

I get the following values ​​from the database:

99, 12, 12.2222, 54.98, 56, 17.556

Now I want to show such values ​​as shown below: 99%, 12%, 12.22%, 54.98%, 56%, 17.55%

Please give me some suggestion to accept this.

+3
source share
5 answers

Very easy in C #:

[EDIT]

var val = 99.569;
string result = string.Format("{0:0.##}%", val);

You can take a look at the method Formatof the string class:
http://msdn.microsoft.com/en-us/library/fht0f5be.aspx

and I recommend that you take a look at user-format strings:
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

+4
source

ToString, - - "P2" # 0. ##%. 100, , , , .

  ToString , "# 0. ## \%", , ToString, , . .

Msdn -
Msdn -

+2

to formart 12.2222 f

 string.Format("{0:f}%", 12.2222); //output 12,22% 
+1

        List<double> myList = new List<double>();
        myList.Add(0.1234);
        myList.Add(99);
        myList.Add(12.1234);
        myList.Add(54.98);
        foreach (double d in myList)
        {
            string First = string.Format("{0:0.00%}", d); //Multiply value by 100
            Console.WriteLine(First);                    
            string Second = string.Format("{0:P}", d);//Multiply value by 100
            Console.WriteLine(Second);  
            string Third = string.Format("{0:P}%", d.ToString());//Use this One 
            Console.WriteLine(Third);  
            string Four = d.ToString() + "%"; //Not a good idea but works
            Console.WriteLine(Four);
            Console.WriteLine("=====================");  
        }

        Console.ReadLine();

{0: P} 100, , % , TOString, {0: }

+1

If you want to specify the number of decimal places by 2 (i.e. not 12.2222%, but 12.22%), use:

val.ToString("0.00") + "%"

Please note that this will be rounded off the number, so 12.226 will be displayed as 12.23%, etc.

0
source

All Articles