One of my requirement required prefix for Number. We need to display the Quantities as the prefix format .ie if the value is 20000 then it will be display like 200k if the value is 2040000 then it will be display like 2.04M (Based on the decimal selection).
Please find the following code
snippet:
In the following code contain two
classes, one for maintaining the prefix format and another static class
maintain the logic for getting the proper prefix. Based on decimal passing we
can change the format.
public static class ShortPrefix
{
private
static List<ShortPrefixInfo> _SIPrefixInfoList = new List<ShortPrefixInfo>();
static
ShortPrefix()
{
_SIPrefixInfoList = new List<ShortPrefixInfo>();
LoadSIPrefix();}
{
get
{
List<ShortPrefixInfo> lstSi = new List<ShortPrefixInfo>();
ShortPrefixInfo[] siPrefixInfoList = new ShortPrefixInfo[5];
_SIPrefixInfoList.CopyTo(siPrefixInfoList);
for (int i = 0; i < siPrefixInfoList.Length; i++)
{
lstSi.Add(siPrefixInfoList[i]);
}
return
lstSi;
}}
private
static void
LoadSIPrefix()
{ShortPrefixInfo SPinfo = new ShortPrefixInfo();
SPinfo.Symbol = "Y";
SPinfo.Prefix = "yotta";
SPinfo.Example = 1000000000000000000000000.00M;
SPinfo.ZeroLength = 24;
SPinfo.ShortScaleName = "Septillion";
SPinfo.LongScaleName = "Quadrillion";
_SIPrefixInfoList.Add(SPinfo);
SPinfo = new ShortPrefixInfo();
SPinfo.Symbol = "Z";
SPinfo.Prefix = "zetta";
SPinfo.Example = 1000000000000000000000M;
SPinfo.ZeroLength = 21;
SPinfo.ShortScaleName = "Sextillion";
SPinfo.LongScaleName = "Trilliard";
_SIPrefixInfoList.Add(SPinfo);
SPinfo = new
ShortPrefixInfo();
SPinfo.Symbol = "E";SPinfo.Prefix = "exa";
SPinfo.Example = 1000000000000000000M;
SPinfo.ZeroLength = 18;
SPinfo.ShortScaleName = "Quintillion";
SPinfo.LongScaleName = "Trillion";
_SIPrefixInfoList.Add(SPinfo);
SPinfo = new
ShortPrefixInfo();
SPinfo.Symbol = "P";SPinfo.Prefix = "peta";
SPinfo.Example = 1000000000000000M;
SPinfo.ZeroLength = 15;
SPinfo.ShortScaleName = "Quadrillion";
SPinfo.LongScaleName = "Billiard";
_SIPrefixInfoList.Add(SPinfo);
SPinfo = new
ShortPrefixInfo();
SPinfo.Symbol = "T";SPinfo.Prefix = "tera";
SPinfo.Example = 1000000000000M;
SPinfo.ZeroLength = 12;
SPinfo.ShortScaleName = "Trillion";
SPinfo.LongScaleName = "Billion";
_SIPrefixInfoList.Add(SPinfo);
SPinfo = new
ShortPrefixInfo();
SPinfo.Symbol = "G";SPinfo.Prefix = "giga";
SPinfo.Example = 1000000000M;
SPinfo.ZeroLength = 9;
SPinfo.ShortScaleName = "Billion";
SPinfo.LongScaleName = "Milliard";
_SIPrefixInfoList.Add(SPinfo);
SPinfo = new
ShortPrefixInfo();
SPinfo.Symbol = "M";SPinfo.Prefix = "mega";
SPinfo.Example = 1000000M;
SPinfo.ZeroLength = 6;
SPinfo.ShortScaleName = "Million";
SPinfo.LongScaleName = "Million";
_SIPrefixInfoList.Add(SPinfo);
SPinfo.Symbol = "K";
SPinfo.Prefix = "kilo";
SPinfo.Example = 1000M;
SPinfo.ZeroLength = 3;
SPinfo.ShortScaleName = "Thousand";
SPinfo.LongScaleName = "Thousand";
_SIPrefixInfoList.Add(SPinfo);
SPinfo = new
ShortPrefixInfo();
SPinfo.Symbol = "H";SPinfo.Prefix = "hecto";
SPinfo.Example = 100M;
SPinfo.ZeroLength = 3;
SPinfo.ShortScaleName = "Hundred";
SPinfo.LongScaleName = "Hundred";
_SIPrefixInfoList.Add(SPinfo);
SPinfo = new
ShortPrefixInfo();
SPinfo.Symbol = "D";SPinfo.Prefix = "deca";
SPinfo.Example = 10M;
SPinfo.ZeroLength = 1;
SPinfo.ShortScaleName = "Ten";
SPinfo.LongScaleName = "Ten";
_SIPrefixInfoList.Add(SPinfo);
SPinfo = new
ShortPrefixInfo();
SPinfo.Symbol = "";SPinfo.Prefix = "";
SPinfo.Example = 1M;
SPinfo.ZeroLength = 0;
SPinfo.ShortScaleName = "one";
SPinfo.LongScaleName = "one";
_SIPrefixInfoList.Add(SPinfo);
}
public static ShortPrefixInfo
GetInfo(long amount, int
decimals)
{
return
GetInfo(Convert.ToDecimal(amount),
decimals);
}
public static ShortPrefixInfo
GetInfo(decimal amount, int decimals)
{
ShortPrefixInfo
siPrefixInfo = null;
decimal
amountToTest = Math.Abs(amount);int amountLength = amountToTest.ToString("0").Length;
if (amountLength < 3)
{
siPrefixInfo = _SIPrefixInfoList.Find(delegate(ShortPrefixInfo sp)
{
return sp.ZeroLength == amountLength; }
).Clone() as ShortPrefixInfo;
siPrefixInfo.AmountWithPrefix =
Math.Round(amount, decimals).ToString();
return
siPrefixInfo;}
siPrefixInfo =
_SIPrefixInfoList.Find(delegate(ShortPrefixInfo sp)
{
return
amountToTest > sp.Example; }
).Clone() as ShortPrefixInfo;
siPrefixInfo.AmountWithPrefix = Math.Round(amountToTest / Convert.ToDecimal(siPrefixInfo.Example),
decimals).ToString() + siPrefixInfo.Symbol;
return
siPrefixInfo;
}
}
{
private string symbol;
private decimal example;
private string prefix;
private int zerolength;
private string shortscalename;
private string longscalename;
private string amountwithprefix;
public string Symbol
{
get
{
return symbol;
}
set
{
symbol = value;
}
}
public decimal Example
{
get{
return example;
}
set
{
example = value;
}
}
public string Prefix {
get
{ return prefix; } set
{ prefix = value; } }public int ZeroLength { get { return zerolength; } set { zerolength = value; } }
public string ShortScaleName { get
{ return shortscalename; } set { shortscalename = value;
} }
public string LongScaleName { get
{ return longscalename; } set { longscalename = value;
} }
public string AmountWithPrefix { get
{ return amountwithprefix; } set { amountwithprefix = value;
} }
public object Clone()
{
ShortPrefixInfo
SPinfo = new ShortPrefixInfo();
SPinfo.Prefix = this.Prefix;
SPinfo.Example = this.Example;
SPinfo.ZeroLength = this.ZeroLength;
SPinfo.ShortScaleName = this.ShortScaleName;
SPinfo.LongScaleName = this.LongScaleName;
return
SPinfo;
}
}
It is very easy to implement. Just
concatenate the column heading with the following method.
cbi.DataColumn.Caption = "Act-" + (ShortPrefix.GetInfo(2000,
2)).AmountWithPrefix.ToString();
Out Put : “Act-2K”
Based on the Desimal value passing
we are rounding the output.
cbi.DataColumn.Caption = "Ro2-" + (ShortPrefix.GetInfo(2040000,
2)).AmountWithPrefix.ToString()
Out Put : “Ro2-2.04M”
No comments:
Post a Comment