Wednesday, May 13, 2015

Sample C# code to shorten amount from 1 Trillion to 1T, 1 Billion to 1 B, 1 Million to 1M and 1 Thousand to 1K.

The following code can be used to shorten a amount from 1 Trillion to 1T, 1 Billion to 1 B, 1 Million to 1M and 1 Thousand to 1K. You can customize it to accept more number of scales amount like Quadrillion, Quintillion, Sextillion, Septillion etc.
public static string GetFormattedAmountText(double amount)
{
 var formattedAmount = string.Empty;

 var x = amount;  //the number to be evaluated
 var e = 0;    //to accumulate the scale
 var i = x;    //a working variable

 while (i > 1)
 {
  i = i / 10;
  e++;
 }

 if (e >= 12)
 {
  formattedAmount = x / 1E9 + "T";
 }
 else if (e >= 9)
 {
  formattedAmount = x / 1E9 + "B";
 }
 else if (e >= 6)
 {
  formattedAmount = x / 1E6 + "M";
 }
 else if (e >= 3)
 {
  formattedAmount = x / 1E3 + "K";
 }

 return formattedAmount;
}

Tuesday, February 10, 2015

Clean way to maintain Session in a single class in ASP.Net application

We can use a wrapper class around the ASP.NET session to maintain session in a single class in ASP.Net application. This class stores one instance of itself in the ASP.NET session and allows you to access your session properties in a type-safe way from any class. Check the below code,
public sealed class Sessions : System.Web.UI.Page
{
     private const string SESSION_MANAGER = "PROJECTNAME_SESSION_OBJECT";

     private Sessions()
     {
          //You can initialize any variables here.
     }

     public static Sessions Current
     {
          get
          {
               Sessions session = (Sessions)HttpContext.Current.Session[SESSION_MANAGER];
               if (session == null)
               {
                     session = new Sessions();
                     HttpContext.Current.Session[SESSION_MANAGER] = session;
               }

               return session;
          }
     }

     // properties
     public string Username{ get; set; }
     public DateTime LoggedinDtae { get; set; }
     public int UserId { get; set; }
}
And you can access those session properties like below,
     //to get UserId
     int userId= Sessions.Current.UserId;
     //to set UserId
     Sessions.Current.UserId = 101;

     //to get Username
     string username= Sessions.Current.Username;
     //to set Username
     Sessions.Current.Username = "test name";

     //to get LoggedinDate
     DateTime loggedinDate = Sessions.Current.LoggedinDate;
     //to set LoggedinDate
     Sessions.Current.LoggedinDate= DateTime.Now;

Wednesday, September 17, 2014

Project Euler Solution using C#: Problem 28 : Number Spiral Diagonals

Problem:

Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13

It can be verified that the sum of the numbers on the diagonals is 101.

What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way? My Solution:

static void Main(string[] args)
{
    static void Main(string[] args)
    {
        int limit = 1001 * 1001;
        int count = 0;
        int loopCount = 2;

        int sum = 1;   //1 coz the centervalue is 1. In loop counting from 3. 
        // every 4 iteration the increment count is adding +2
        for (int i = 3; i <= limit; i += loopCount)
        {
            sum += i;
            count += 1;
            if (count >= 4)
            {
                loopCount += 2;
                count = 0;
            }
        }
        Console.WriteLine(sum);
        Console.ReadLine();
    }
}

Note: You can simplifies the coding :)