Last active
May 9, 2018 19:18
-
-
Save SajjadArifGul/5f5fe7e7aee35eb7f446d8c06916d201 to your computer and use it in GitHub Desktop.
String Based Number Formatting v1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ParseNumberByString | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
decimal Number; | |
string formattingString = @"RoundOff=2:CommaSeparator=true:value,#black:(value),#red:if>50=HIGH,#green:if<-50=LOW,#red"; | |
Number = 0; | |
Console.WriteLine(ParseNumberByString(formattingString, Number)); | |
//output: 0.00, | |
Number = 31; | |
Console.WriteLine(ParseNumberByString(formattingString, Number)); | |
//output: 31.00, black | |
Number = -15; | |
Console.WriteLine(ParseNumberByString(formattingString, Number)); | |
//output: (15.00), red | |
Number = 53; | |
Console.WriteLine(ParseNumberByString(formattingString, Number)); | |
//output: HIGH, green | |
Number = -89; | |
Console.WriteLine(ParseNumberByString(formattingString, Number)); | |
//output: LOW, red | |
//---------------------------------------------------------------- | |
formattingString = @"RoundOff=2:CommaSeparator=true:value,#black:(value),#red"; | |
Number = 25698442.36m; | |
Console.WriteLine(ParseNumberByString(formattingString, Number)); | |
//output: 25,698,442.36, black | |
Number = -6316464.45m; | |
Console.WriteLine(ParseNumberByString(formattingString, Number)); | |
//output: (6,316,464.45), red | |
} | |
static string ParseNumberByString(string Formatting, decimal Number) | |
{ | |
if(!string.IsNullOrEmpty(Formatting)) | |
{ | |
string Value = "value"; | |
string ValueColor = ""; | |
var Formats = Formatting.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries).ToList(); | |
//at 0 index we will have formatting for Roundoff | |
if (!string.IsNullOrEmpty(Formats[0])) | |
{ | |
int NumberOfDecimalPoints = 2; | |
int.TryParse(GetSubstring(Formats[0], "="), out NumberOfDecimalPoints); | |
Number = decimal.Round(Number, NumberOfDecimalPoints, MidpointRounding.AwayFromZero); | |
} | |
if (Number > 0) | |
{ | |
//at 2 index we will have formatting for Positive | |
if (!string.IsNullOrEmpty(Formats[2])) | |
{ | |
var PositiveFormats = Formats[2].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); | |
Value = PositiveFormats[0]; | |
ValueColor = GetSubstring(PositiveFormats[1], "#"); | |
} | |
} | |
else if (Number < 0) | |
{ | |
//at 3 index we will have formatting for Positive | |
if (!string.IsNullOrEmpty(Formats[3])) | |
{ | |
var NegativeFormats = Formats[3].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); | |
Value = NegativeFormats[0]; | |
ValueColor = GetSubstring(NegativeFormats[1], "#"); | |
} | |
} | |
//if present, at 4 index we will have formatting for Greater Than Condition | |
if (Formats.Count > 4 && !string.IsNullOrEmpty(Formats[4])) | |
{ | |
var GreaterThanNumber = int.Parse(GetSubstring(Formats[4], ">", "=")); | |
if(Number > GreaterThanNumber) | |
{ | |
var GreaterFormats = Formats[4].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); | |
Value = GetSubstring(GreaterFormats[0], "="); | |
ValueColor = GetSubstring(GreaterFormats[1], "#"); | |
} | |
} | |
//if present, at 5 index we will have formatting for Lower Than Condition | |
if (Formats.Count > 5 && !string.IsNullOrEmpty(Formats[5])) | |
{ | |
var LowerThanNumber = int.Parse(GetSubstring(Formats[5], "<", "=")); | |
if (Number < LowerThanNumber) | |
{ | |
var LowerFormats = Formats[5].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); | |
Value = GetSubstring(LowerFormats[0], "="); | |
ValueColor = GetSubstring(LowerFormats[1], "#"); | |
} | |
} | |
//at 1 index we will have formatting for CommaSeparator | |
if (!string.IsNullOrEmpty(Formats[1]) && GetSubstring(Formats[1], "=").ToLower().Equals("true")) | |
{ | |
Value = Value.Replace("value", Number.ToString("N2")).Replace("-", ""); | |
} | |
else | |
{ | |
Value = Value.Replace("value", Number.ToString()).Replace("-", ""); | |
} | |
return string.Format("{0}, {1}", Value, ValueColor); | |
} | |
else | |
{ | |
return Number.ToString(); | |
} | |
} | |
static string GetSubstring(string Str, string StartingCharacter, string EndingCharacter = "") | |
{ | |
var Start = Str.IndexOf(StartingCharacter) + 1; | |
var End = !string.IsNullOrEmpty(EndingCharacter) ? Str.IndexOf(EndingCharacter) - Start : 0; | |
if (End > 0) | |
return Str.Substring(Start, End); | |
else return Str.Substring(Start); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment