static public string Format(object value, Section node, CultureInfo culture) { switch (node.Type) { case SectionType.Number: // Hide sign under certain conditions and section index var number = Convert.ToDouble(value, culture); if ((node.SectionIndex == 0 && node.Condition != null) || node.SectionIndex == 1) { number = Math.Abs(number); } return(FormatNumber(number, node.Number, culture)); case SectionType.Date: return(FormatDate(Convert.ToDateTime(value, culture), node.GeneralTextDateDurationParts, culture)); case SectionType.Duration: return(FormatTimeSpan((TimeSpan)value, node.GeneralTextDateDurationParts, culture)); case SectionType.General: case SectionType.Text: return(FormatGeneralText(CompatibleConvert.ToString(value, culture), node.GeneralTextDateDurationParts)); case SectionType.Exponential: return(FormatExponential(Convert.ToDouble(value, culture), node, culture)); case SectionType.Fraction: return(FormatFraction(Convert.ToDouble(value, culture), node, culture)); default: throw new InvalidOperationException("Unknown number format section"); } }
static public string Format(object value, Section node, CultureInfo culture, bool isDate1904) { switch (node.Type) { case SectionType.Number: // Hide sign under certain conditions and section index var number = Convert.ToDouble(value, culture); if ((node.SectionIndex == 0 && node.Condition != null) || node.SectionIndex == 1) { number = Math.Abs(number); } return(FormatNumber(number, node.Number, culture)); case SectionType.Date: if (ExcelDateTime.TryConvert(value, isDate1904, culture, out var excelDateTime)) { return(FormatDate(excelDateTime, node.GeneralTextDateDurationParts, culture)); } else { throw new FormatException("Unexpected date value"); } case SectionType.Duration: if (value is TimeSpan ts) { return(FormatTimeSpan(ts, node.GeneralTextDateDurationParts, culture)); } else { var d = Convert.ToDouble(value); return(FormatTimeSpan(TimeSpan.FromDays(d), node.GeneralTextDateDurationParts, culture)); } case SectionType.General: case SectionType.Text: return(FormatGeneralText(CompatibleConvert.ToString(value, culture), node.GeneralTextDateDurationParts)); case SectionType.Exponential: return(FormatExponential(Convert.ToDouble(value, culture), node, culture)); case SectionType.Fraction: return(FormatFraction(Convert.ToDouble(value, culture), node, culture)); default: throw new InvalidOperationException("Unknown number format section"); } }
static public string Format(object value, string formatString, CultureInfo culture) { var format = new NumberFormat(formatString); if (!format.IsValid) { return(CompatibleConvert.ToString(value, culture)); } var section = Evaluator.GetSection(format.Sections, value); if (section == null) { return(CompatibleConvert.ToString(value, culture)); } return(Format(value, section, culture)); }
/// <summary> /// Formats a value with this number format in a specified culture. /// </summary> /// <param name="value">The value to format.</param> /// <param name="culture">The culture to use for formatting.</param> /// <returns>The formatted string.</returns> public string Format(object value, CultureInfo culture) { var section = Evaluator.GetSection(Sections, value); if (section == null) { return(CompatibleConvert.ToString(value, culture)); } try { return(Formatter.Format(value, section, culture)); } catch (InvalidCastException) { // TimeSpan cast exception return(CompatibleConvert.ToString(value, culture)); } catch (FormatException) { // Convert.ToDouble/ToDateTime exceptions return(CompatibleConvert.ToString(value, culture)); } }