Format string to double, int, digits and currency format in C#.net

In this section i am explaining how to convert string to different formats.
In the below example i have shown

  • How to convert string to decimal format using C#.net.
  • How to convert string to double using C#.net
  • How to convert string to integer using C#.net
  • How to convert string to currency format using c#.net.
  • How to convert string to different different formats. 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringFormats
{
    class Program
    {
        static void Main(string[] args)
        {
            //string format format upto two decimal places. 
            Console.WriteLine(String.Format("{0:0.00}", 545.4578));      // "545.46"
            Console.WriteLine(String.Format("{0:0.00}", 545.4));         // "545.40"
            Console.WriteLine(String.Format("{0:0.00}", 545.0));         // "545.00"

            // maintain minimum two digits before dot
            Console.WriteLine(String.Format("{0:00.0}", 545.4545));      // "545.5"
            Console.WriteLine(String.Format("{0:00.0}", 545.4567));       // "545.5"
            Console.WriteLine(String.Format("{0:00.0}", 3.545));        // "03.5"
            Console.WriteLine(String.Format("{0:00.0}", -3.545));       // "-03.5"

            //indian rupees string format. 
            Console.WriteLine(String.Format("{0:0,0.0}", 54555.67));     // "54,555.7"
            Console.WriteLine(String.Format("{0:0,0}", 54545.67));       // "54,545"

            //String operations on zero
            Console.WriteLine(String.Format("{0:0.0}", 0.0));            // "0.0"
            Console.WriteLine(String.Format("{0:0.#}", 0.0));            // "0"
            Console.WriteLine(String.Format("{0:#.0}", 0.0));            // ".0"
            Console.WriteLine(String.Format("{0:#.#}", 0.0));            // ""
            Console.WriteLine(String.Format("{0:0.00;minus 0.00;zero}", 0.0));        // "zero"

            //string operations on placements. 
            Console.WriteLine(String.Format("{0,10:0.0}", 545.4567));    // "     545.5"
            Console.WriteLine(String.Format("{0,-10:0.0}", 545.4567));   // "545.5     "
            Console.WriteLine(String.Format("{0,10:0.0}", -545.4567));   // "    -545.5"
            Console.WriteLine(String.Format("{0,-10:0.0}", -545.4567));  // "-545.5    "

            //writing minus for negative values. 
            Console.WriteLine(String.Format("{0:0.00;minus 0.00;zero}", 545.4567));   // "545.46"
            Console.WriteLine(String.Format("{0:0.00;minus 0.00;zero}", -545.4567));  // "minus 545.46"

            //format with data. 
            Console.WriteLine(String.Format("{0:my Salary is 0.0}", 9565.3));   // "my Salary is 9565.3"
            Console.WriteLine(String.Format("{0:0Ranga.rajesh0}", 12.3));          // "12Ranga.rajesh3"

            Console.ReadLine();
        }
    }
}

Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar