public static string ToString(double value, string exception, double min, double max, char separator) { string output = Doubles.ToString(value, exception, min, max); if (System.String.IsNullOrEmpty(output)) { return(output); } string correction = string.Empty; bool IsSeparatorFound = false; for (int i = 0; i < output.Length; i++) { char c = output[i]; if (char.IsDigit(c)) { correction += c; } if (!IsSeparatorFound) { correction += separator; IsSeparatorFound = true; } } return(correction); }
/// <summary> /// Colours textbox RGB depending on value /// </summary> /// <param name="Tbx"></param> /// <param name="value"></param> /// <param name="exception"></param> /// <param name="decimals"></param> /// <param name="min"></param> /// <param name="max"></param> /// <param name="punctuation"></param> public static void ColourDouble(ref TextBox Tbx, double value, string exception, int decimals, double min, double max, char punctuation, double precision = 0) { Tbx.Text = Doubles.ToString(value, exception, decimals, min, max, punctuation); if (Tbx.Text == exception) { Tbx.ForeColor = Color.Black; } else if (AMath.Equ(value, 0, precision)) { Tbx.ForeColor = Color.DarkBlue; } else if (value < 0) { Tbx.ForeColor = Color.DarkRed; } else if (value > 0) { Tbx.ForeColor = Color.DarkGreen; } }
/// <summary> /// This method parses string to double ignoring white spaces and changing first puctuation into specified one /// </summary> /// <param name="value"></param> /// <param name="exception"></param> /// <param name="decimals"></param> /// <param name="min"></param> /// <param name="max"></param> /// <param name="separator"></param> /// <returns></returns> public static string ToString(double value, string exception, int decimals, double min, double max, char punctuation) { string output = Doubles.ToString(value, exception, decimals, min, max); if (System.String.IsNullOrEmpty(output)) { return(output); } string sign = ""; if (output[0] == '-') { sign = "-"; output = output.Substring(1, output.Length - 1); } string correction = string.Empty; bool IsSeparatorFound = false; for (int i = 0; i < output.Length; i++) { char c = output[i]; if (char.IsDigit(c)) { correction += c; continue; } if (!IsSeparatorFound && (c == '.' || c == ',')) { correction += punctuation; IsSeparatorFound = true; } } return(sign + correction); }
/// <summary> /// Colours texbox RGB depending on values comparison and assigns v1 /// </summary> /// <param name="Tbx"></param> /// <param name="v1"></param> /// <param name="v2"></param> /// <param name="exception"></param> /// <param name="decimals"></param> /// <param name="min"></param> /// <param name="max"></param> /// <param name="punctuation"></param> public static void ColourDouble(ref TextBox Tbx, double v1, double v2, string exception, int decimals, double min, double max, char punctuation, double precision = 0) { FormsControls.ColourDouble(ref Tbx, v1, v1, v2, exception, decimals, min, max, punctuation, precision); Tbx.Text = Doubles.ToString(v1, exception, decimals, min, max, punctuation); }
/// <summary> /// Parses duble with check of digit format /// Fails: 3 /// 16.76 /// 19,315 /// </summary> /// <param name="input"></param> /// <param name="digits"></param> /// <returns></returns> public static double Parse(string input, int decimals) { input = input.Replace(" ", ""); //Remove white spaces string sign = ""; if (input[0] == '-') { sign = "-"; input = input.Substring(1, input.Length - 1); } int position = input.Length - (decimals + 1); int cntd1 = Doubles.CharsCount(input, '.'); int cntd2 = Doubles.CharsCount(input, ','); bool exc = false; if (cntd1 + cntd2 == 1 && decimals != 0) { exc = true; } string correction = ""; for (int i = 0; i < input.Length; i++) { char c = input[i]; //if char is a digit, add it and simply continue if (char.IsDigit(c)) { correction += c; continue; } if (c == '.' || c == ',') { //ignore excess formating if (i == position || exc) { correction += c; } continue; } throw new Exception("Unknown Format !"); } correction = correction.Replace(",", "."); correction = sign + correction; double value = double.Parse(correction, CultureInfo.InvariantCulture); if (sign == "-") { value = -value; } #region Bact Test //new number of decimals cannot be greater then expected string test = value.ToString().Replace(",", "."); int index = test.IndexOf("."); if (index >= 0) { int dec = test.Length - (index + 1); if (dec > decimals) { throw new Exception("Backtest failed !"); } } #endregion return(value); }