public string ToString(HourAngleFormat format) { string text = string.Empty; int hours; int minutes; double doubleMinutes; double seconds; int increment; switch (format) { case HourAngleFormat.DecimalHours: text = CustomFormat.ToString(Resources.HourAngleInDecimalHours, _NumberDecimalDigitsForHours, Value); break; case HourAngleFormat.HoursDecimalMinutes: increment = (Value >= 0.0 ? 1 : -1); doubleMinutes = Math.Round((double)Minutes + (Seconds / 60.0), _NumberDecimalDigitsForHours); if (Math.Abs(doubleMinutes) < 60.0) { hours = Hours; } else { doubleMinutes = 0.0; hours = Hours + increment; } text = CustomFormat.ToString(Resources.HourAngleInHoursDecimalMinutes, _NumberDecimalDigitsForHours, hours, doubleMinutes); break; case HourAngleFormat.HoursMinutesSeconds: case HourAngleFormat.CadHoursMinutesSeconds: case HourAngleFormat.CompactHoursMinutesSeconds: increment = (Value >= 0.0 ? 1 : -1); seconds = Math.Round(Seconds, format != HourAngleFormat.CompactHoursMinutesSeconds ? _NumberDecimalDigitsForSeconds : HourAngle.NumberDecimalDigitsForCompactSeconds); if (Math.Abs(seconds) < 60.0) { minutes = Minutes; } else { seconds = 0.0; minutes = Minutes + increment; } if (Math.Abs(minutes) < 60) { hours = Hours; } else { minutes = 0; hours = Hours + increment; } if (format == HourAngleFormat.HoursMinutesSeconds) { text = CustomFormat.ToString(Resources.HourAngleInHoursMinutesSeconds, _NumberDecimalDigitsForSeconds, hours, minutes, seconds); } else if (format == HourAngleFormat.CompactHoursMinutesSeconds) { text = CustomFormat.ToString(Resources.HoursAngleInCompactHoursMinutesSeconds, _NumberDecimalDigitsForSeconds, hours, minutes, seconds); } else { /* Because 'CAD' coordinates use a comma as the lat/long delimiter, a period must * be used as the double point, hence the use of the InvariantCulture. */ text = CustomFormat.ToString(CultureInfo.InvariantCulture, Resources.HourAngleInCadHoursMinutesSeconds, _NumberDecimalDigitsForSeconds, hours, minutes, seconds); } break; default: Debug.Assert(format == HourAngleFormat.NotSpecified, "Unrecognised HourAngleFormat value - " + format.ToString()); text = CustomFormat.ToString(Resources.HourAngleWithNoFormat, _NumberDecimalDigitsForHours, Value); break; } return(text); }
public HourAngle(string hour) { /* All members must be set before calling out of the constructor so set dummy values * so compiler is happy. */ _Value = 0.0; _Hours = 0; _Minutes = 0; _Seconds = 0.0; _Format = HourAngleFormat.NotSpecified; _HasBeenSet = false; /* The 'CAD' format is checked first against the InvariantCulture; it uses a comma as * the lat/long delimiter, so a period must be used as the double point. */ foreach (Regex regex in _CadRegexes) { Match match = regex.Match(hour); if (match.Success) { _Hours = System.Convert.ToInt32(match.Groups["Hrs"].Value, CultureInfo.InvariantCulture); _Minutes = System.Convert.ToInt32(match.Groups["Mins"].Value, CultureInfo.InvariantCulture); _Seconds = System.Convert.ToDouble(match.Groups["Secs"].Value, CultureInfo.InvariantCulture); _Value = SetHoursFromHms(); _Format = HourAngleFormat.CadHoursMinutesSeconds; _HasBeenSet = true; break; } } /* The remaining formats can be locale-specific, but the regex patterns have to be * hardcoded with a period as the double point. So to keep things simple, we'll * just replace the locale-specific separator with the invariant one. (Don't need * to worry about grouping characters, etc, since the numbers shouldn't be that big.) */ hour = hour.Replace(CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator, "."); if (!_HasBeenSet) { foreach (Regex regex in _HrsRegexes) { Match match = regex.Match(hour); if (match.Success) { _Value = System.Convert.ToDouble(match.Groups["Hrs"].Value, CultureInfo.InvariantCulture); SetHmsFromHours(_Value); _Format = HourAngleFormat.DecimalHours; _HasBeenSet = true; break; } } } if (!_HasBeenSet) { foreach (Regex regex in _HdmRegexes) { Match match = regex.Match(hour); if (match.Success) { double minutes = 0.0; _Hours = System.Convert.ToInt32(match.Groups["Hrs"].Value, CultureInfo.InvariantCulture); minutes = System.Convert.ToDouble(match.Groups["Mins"].Value, CultureInfo.InvariantCulture); _Minutes = (int)Truncate(minutes); _Seconds = (minutes - (double)Minutes) * 60.0; _Value = SetHoursFromHms(); _Format = HourAngleFormat.HoursDecimalMinutes; _HasBeenSet = true; break; } } } if (!_HasBeenSet) { foreach (Regex regex in _HmsRegexes) { Match match = regex.Match(hour); if (match.Success) { _Hours = System.Convert.ToInt32(match.Groups["Hrs"].Value, CultureInfo.InvariantCulture); _Minutes = System.Convert.ToInt32(match.Groups["Mins"].Value, CultureInfo.InvariantCulture); _Seconds = System.Convert.ToDouble(match.Groups["Secs"].Value, CultureInfo.InvariantCulture); _Value = SetHoursFromHms(); _Format = HourAngleFormat.HoursMinutesSeconds; _HasBeenSet = true; break; } } } if (!_HasBeenSet) { throw new FormatException("Invalid hour format."); } }