Wednesday, May 13, 2015

Sample C# code to Generate linear Y axis value for a chart from the given amount.

Below is a sample C# code to Generate linear Y axis value for a chart from the given amount. You wil get the lower bound of the y axis value when you call the CalculateLowerBound method. That will be added itself based on the barCount in a loop to get the linear y axis values.
public List GenerateAxis()
{
 int barCount = 5;
 double amount = 10000;
 double money = CalculateLowerBound(amount, barCount);

 var axisVal = new List();

 double valX = 0;
 for (int i = 1; i <= barCount; i++)
 {
  valX += money;
  axisVal.Add(valX);
 }

 return axisVal;
}

public double CalculateLowerBound(double range, int barCount)
{
 double unroundedTickSize = range / (barCount);
 double x = Math.Ceiling(Math.Log10(unroundedTickSize) - 1);
 double pow10x = Math.Pow(10, x);
 return Math.Ceiling(unroundedTickSize / pow10x) * pow10x;
}

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;