It seems that some additional tuning is needed before continue to program. The issue is that Croatia uses decimal comma and conversion string to double using Convert.ToDouble does not work fine since decimal comma is part of string. Solution with using System.Globalization; did not provide satisfactory results when converting to Double (much faster then Decimal). So, commaCountry constant string was introduced to define if we use comma as decimal sign.
After, conversion string to double is done, some additional math is done: product, sum and difference. It was an excellent opportunity to use class and its properties to return results. Bellow is difference between old and new version of routine dayColor.
Class calC is part of Globals.
class calC
{
public calC(string firsT, string seconD) // calC class - input two numbers as string - string to double for countries
{ // using decimal comma
double mlaF; // auxiliary number for the first day
double mlaS; // auxiliary number for the second day
if (firsT.Contains(".")) // if string contains point
{
if (Globals.commaCountry == Globals.yeS) // if country uses decimal comma
{ mlaF = Convert.ToDouble(firsT.Replace(".", ",")); } // and if you use decimal comma we need fix
}
else
{ mlaF = Convert.ToDouble(firsT); } // convert string to doubleif (firsT.Contains("."))
if (seconD.Contains(".")) // the same as above for first number
{
if (Globals.commaCountry == Globals.yeS)
{ mlaS = Convert.ToDouble(seconD.Replace(".", ",")); } // but if you use decimal comma we need fix
}
else
{ mlaS = Convert.ToDouble(seconD); } // convert string to doubleif (firsT.Contains("."))
double mlaPro = Math.Round( mlaF * mlaS); // product rounded to two decimal places
double mlaDiffFirstMinSecond = Math.Round(mlaF - mlaS); // difference first minus second rounded to two decimal places
double mlaSum = Math.Round(mlaF + mlaS); // first plus second rounded to two decimal places
this.producT = mlaPro; // define properties
this.diffFmS = mlaDiffFirstMinSecond;
this.sumFS = mlaSum;
}
public double producT { get; private set; }
public double diffFmS { get; private set; }
public double sumFS { get; private set; }
}
How is called;
old:
private string dayColor( string[] aR)
{
string mE = "Day color : "; // name of routine
string coloR; // color of the day
double openP; // opening price
double closeR; // closing price
openP = stringToDouble(aR[1]); // covert string to double
closeR=stringToDouble(aR[2]);
double closeMinusOpen = closeR - openP; // covert string to double
if (closeMinusOpen > 0) // is close price bigger then oper
{ coloR = Globals.whitE; } // yes, white day
else if (closeMinusOpen < 0) { coloR = Globals.blacK; } // no, black day
else { coloR = Globals.nO; } // both prices are the same -> no color
if (showDetails.Checked) { analyseResults.Items.Add(mE +aR[0] + " -> " + coloR); }
return coloR;
}
new:
private string dayColor( string[] aR)
{
string mE = "Day color : "; // name of routine
string coloR = "Color is not calculated - ERROR" ; // color of the day, by defalt ERROR message which should be changed later
calC cC = new calC(aR[1], aR[2]); // calC class computes two strings and returns double
if (cC.diffFmS > 0) { coloR = Globals.blacK; }
if (cC.diffFmS < 0) { coloR = Globals.whitE; }
if (cC.diffFmS == 0) { coloR = Globals.nO; }
if (showDetails.Checked) { analyseResults.Items.Add(mE +aR[0] + " -> " + coloR); } // show result if checked
return coloR;
}
No comments:
Post a Comment