NumberFormat.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ComponentModel;
  5. using FastReport.Utils;
  6. using System.Globalization;
  7. namespace FastReport.Format
  8. {
  9. /// <summary>
  10. /// Defines how numeric values are formatted and displayed.
  11. /// </summary>
  12. public class NumberFormat : FormatBase
  13. {
  14. #region Fields
  15. private bool useLocale;
  16. private int decimalDigits;
  17. private string decimalSeparator;
  18. private string groupSeparator;
  19. private int negativePattern;
  20. #endregion
  21. #region Properties
  22. /// <summary>
  23. /// Gets or sets a value that determines whether to use system locale settings to format a value.
  24. /// </summary>
  25. [DefaultValue(true)]
  26. public bool UseLocale
  27. {
  28. get { return useLocale; }
  29. set { useLocale = value; }
  30. }
  31. /// <summary>
  32. /// Gets or sets the number of decimal places to use in numeric values.
  33. /// </summary>
  34. [DefaultValue(2)]
  35. public int DecimalDigits
  36. {
  37. get { return decimalDigits; }
  38. set { decimalDigits = value; }
  39. }
  40. /// <summary>
  41. /// Gets or sets the string to use as the decimal separator in numeric values.
  42. /// </summary>
  43. public string DecimalSeparator
  44. {
  45. get { return decimalSeparator; }
  46. set { decimalSeparator = value; }
  47. }
  48. /// <summary>
  49. /// Gets or sets the string that separates groups of digits to the left of the decimal in numeric values.
  50. /// </summary>
  51. public string GroupSeparator
  52. {
  53. get { return groupSeparator; }
  54. set { groupSeparator = value; }
  55. }
  56. /// <summary>
  57. /// Gets or sets the format pattern for negative numeric values.
  58. /// </summary>
  59. /// <remarks>This property can have one of the values in the following table.
  60. /// The symbol <i>n</i> is a number.
  61. /// <list type="table">
  62. /// <listheader><term>Value</term><description>Associated Pattern</description></listheader>
  63. /// <item><term>0</term><description>(n)</description></item>
  64. /// <item><term>1</term><description>-n</description></item>
  65. /// <item><term>2</term><description>- n</description></item>
  66. /// <item><term>3</term><description>n-</description></item>
  67. /// <item><term>4</term><description>n -</description></item>
  68. /// </list>
  69. /// </remarks>
  70. [DefaultValue(0)]
  71. public int NegativePattern
  72. {
  73. get { return negativePattern; }
  74. set { negativePattern = value; }
  75. }
  76. #endregion
  77. #region Public Methods
  78. /// <inheritdoc/>
  79. public override FormatBase Clone()
  80. {
  81. NumberFormat result = new NumberFormat();
  82. result.UseLocale = UseLocale;
  83. result.DecimalDigits = DecimalDigits;
  84. result.DecimalSeparator = DecimalSeparator;
  85. result.GroupSeparator = GroupSeparator;
  86. result.NegativePattern = NegativePattern;
  87. return result;
  88. }
  89. /// <inheritdoc/>
  90. public override bool Equals(object obj)
  91. {
  92. NumberFormat f = obj as NumberFormat;
  93. return f != null &&
  94. UseLocale == f.UseLocale &&
  95. DecimalDigits == f.DecimalDigits &&
  96. DecimalSeparator == f.DecimalSeparator &&
  97. GroupSeparator == f.GroupSeparator &&
  98. NegativePattern == f.NegativePattern;
  99. }
  100. /// <inheritdoc/>
  101. public override int GetHashCode()
  102. {
  103. return base.GetHashCode();
  104. }
  105. /// <inheritdoc/>
  106. public override string FormatValue(object value)
  107. {
  108. if (value is Variant)
  109. value = ((Variant)value).Value;
  110. return String.Format(GetNumberFormatInfo(), "{0:n}", new object[] { value });
  111. }
  112. internal NumberFormatInfo GetNumberFormatInfo()
  113. {
  114. NumberFormatInfo info = new NumberFormatInfo();
  115. if (UseLocale)
  116. {
  117. NumberFormatInfo cultureFormat = CultureInfo.CurrentCulture.NumberFormat;
  118. info.NumberDecimalDigits = DecimalDigits;
  119. info.NumberDecimalSeparator = cultureFormat.NumberDecimalSeparator;
  120. info.NumberGroupSizes = cultureFormat.NumberGroupSizes;
  121. info.NumberGroupSeparator = cultureFormat.NumberGroupSeparator;
  122. info.NumberNegativePattern = cultureFormat.NumberNegativePattern;
  123. }
  124. else
  125. {
  126. info.NumberDecimalDigits = DecimalDigits;
  127. info.NumberDecimalSeparator = DecimalSeparator;
  128. info.NumberGroupSizes = new int[] { 3 };
  129. info.NumberGroupSeparator = GroupSeparator;
  130. info.NumberNegativePattern = NegativePattern;
  131. }
  132. return info;
  133. }
  134. internal override string GetSampleValue()
  135. {
  136. return FormatValue(-12345f);
  137. }
  138. internal override void Serialize(FRWriter writer, string prefix, FormatBase format)
  139. {
  140. base.Serialize(writer, prefix, format);
  141. NumberFormat c = format as NumberFormat;
  142. if (c == null || UseLocale != c.UseLocale)
  143. writer.WriteBool(prefix + "UseLocale", UseLocale);
  144. if (c == null || DecimalDigits != c.DecimalDigits)
  145. writer.WriteInt(prefix + "DecimalDigits", DecimalDigits);
  146. if (!UseLocale)
  147. {
  148. if (c == null || DecimalSeparator != c.DecimalSeparator)
  149. writer.WriteStr(prefix + "DecimalSeparator", DecimalSeparator);
  150. if (c == null || GroupSeparator != c.GroupSeparator)
  151. writer.WriteStr(prefix + "GroupSeparator", GroupSeparator);
  152. if (c == null || NegativePattern != c.NegativePattern)
  153. writer.WriteInt(prefix + "NegativePattern", NegativePattern);
  154. }
  155. }
  156. #endregion
  157. /// <summary>
  158. /// Initializes a new instance of the <b>NumberFormat</b> class with default settings.
  159. /// </summary>
  160. public NumberFormat()
  161. {
  162. UseLocale = true;
  163. DecimalDigits = 2;
  164. DecimalSeparator = ".";
  165. GroupSeparator = ",";
  166. }
  167. }
  168. }