using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; using FastReport.Utils; using System.Globalization; namespace FastReport.Format { /// /// Defines how numeric values are formatted and displayed. /// public class NumberFormat : FormatBase { #region Fields private bool useLocale; private int decimalDigits; private string decimalSeparator; private string groupSeparator; private int negativePattern; #endregion #region Properties /// /// Gets or sets a value that determines whether to use system locale settings to format a value. /// [DefaultValue(true)] public bool UseLocale { get { return useLocale; } set { useLocale = value; } } /// /// Gets or sets the number of decimal places to use in numeric values. /// [DefaultValue(2)] public int DecimalDigits { get { return decimalDigits; } set { decimalDigits = value; } } /// /// Gets or sets the string to use as the decimal separator in numeric values. /// public string DecimalSeparator { get { return decimalSeparator; } set { decimalSeparator = value; } } /// /// Gets or sets the string that separates groups of digits to the left of the decimal in numeric values. /// public string GroupSeparator { get { return groupSeparator; } set { groupSeparator = value; } } /// /// Gets or sets the format pattern for negative numeric values. /// /// This property can have one of the values in the following table. /// The symbol n is a number. /// /// ValueAssociated Pattern /// 0(n) /// 1-n /// 2- n /// 3n- /// 4n - /// /// [DefaultValue(0)] public int NegativePattern { get { return negativePattern; } set { negativePattern = value; } } #endregion #region Public Methods /// public override FormatBase Clone() { NumberFormat result = new NumberFormat(); result.UseLocale = UseLocale; result.DecimalDigits = DecimalDigits; result.DecimalSeparator = DecimalSeparator; result.GroupSeparator = GroupSeparator; result.NegativePattern = NegativePattern; return result; } /// public override bool Equals(object obj) { NumberFormat f = obj as NumberFormat; return f != null && UseLocale == f.UseLocale && DecimalDigits == f.DecimalDigits && DecimalSeparator == f.DecimalSeparator && GroupSeparator == f.GroupSeparator && NegativePattern == f.NegativePattern; } /// public override int GetHashCode() { return base.GetHashCode(); } /// public override string FormatValue(object value) { if (value is Variant) value = ((Variant)value).Value; return String.Format(GetNumberFormatInfo(), "{0:n}", new object[] { value }); } internal NumberFormatInfo GetNumberFormatInfo() { NumberFormatInfo info = new NumberFormatInfo(); if (UseLocale) { NumberFormatInfo cultureFormat = CultureInfo.CurrentCulture.NumberFormat; info.NumberDecimalDigits = DecimalDigits; info.NumberDecimalSeparator = cultureFormat.NumberDecimalSeparator; info.NumberGroupSizes = cultureFormat.NumberGroupSizes; info.NumberGroupSeparator = cultureFormat.NumberGroupSeparator; info.NumberNegativePattern = cultureFormat.NumberNegativePattern; } else { info.NumberDecimalDigits = DecimalDigits; info.NumberDecimalSeparator = DecimalSeparator; info.NumberGroupSizes = new int[] { 3 }; info.NumberGroupSeparator = GroupSeparator; info.NumberNegativePattern = NegativePattern; } return info; } internal override string GetSampleValue() { return FormatValue(-12345f); } internal override void Serialize(FRWriter writer, string prefix, FormatBase format) { base.Serialize(writer, prefix, format); NumberFormat c = format as NumberFormat; if (c == null || UseLocale != c.UseLocale) writer.WriteBool(prefix + "UseLocale", UseLocale); if (c == null || DecimalDigits != c.DecimalDigits) writer.WriteInt(prefix + "DecimalDigits", DecimalDigits); if (!UseLocale) { if (c == null || DecimalSeparator != c.DecimalSeparator) writer.WriteStr(prefix + "DecimalSeparator", DecimalSeparator); if (c == null || GroupSeparator != c.GroupSeparator) writer.WriteStr(prefix + "GroupSeparator", GroupSeparator); if (c == null || NegativePattern != c.NegativePattern) writer.WriteInt(prefix + "NegativePattern", NegativePattern); } } #endregion /// /// Initializes a new instance of the NumberFormat class with default settings. /// public NumberFormat() { UseLocale = true; DecimalDigits = 2; DecimalSeparator = "."; GroupSeparator = ","; } } }