TextFormatting.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using Microsoft.VisualBasic;
  2. using Microsoft.VisualBasic.CompilerServices;
  3. using System;
  4. namespace MuchInfo.Chart.WPF.Helpers
  5. {
  6. public class TextFormatting
  7. {
  8. /// <summary>
  9. /// Formats the number.
  10. /// </summary>
  11. /// <param name="aVal">A value.</param>
  12. /// <param name="significantDigits">The significant digits.</param>
  13. /// <param name="allowAbbreviate">if set to <c>true</c> [allow abbreviate].</param>
  14. /// <returns>System.Object.</returns>
  15. public static string FormatNumber(float aVal, int significantDigits, bool allowAbbreviate)
  16. {
  17. checked
  18. {
  19. string result;
  20. try
  21. {
  22. bool flag = double.IsNaN(aVal) || double.IsInfinity(aVal);
  23. if (flag)
  24. {
  25. result = "";
  26. return result;
  27. }
  28. string text = "F" + significantDigits;
  29. //int length = ((long)Math.Round(Math.Ceiling(aVal))).ToString().Length;
  30. //string text = "###0.00";
  31. //int num = significantDigits - 1;
  32. //int num2 = length + 2;
  33. //while (true)
  34. //{
  35. // if (num2 > num)
  36. // {
  37. // break;
  38. // }
  39. // text += "0";
  40. // num2++;
  41. //}
  42. if (allowAbbreviate)
  43. {
  44. result = GetAbbreviatedValueWithUnit(aVal, text);
  45. return result;
  46. }
  47. result = Strings.Format(aVal, text);
  48. return result;
  49. }
  50. catch
  51. {
  52. result = Strings.Format(aVal, "F2");
  53. return result;
  54. }
  55. }
  56. }
  57. public static string GetAbbreviatedValueWithUnit(double aVal, string longFormat)
  58. {
  59. string result = Strings.Format(aVal, "0.00");
  60. bool flag = Operators.CompareString(longFormat, "", false) != 0;
  61. if (flag)
  62. {
  63. try
  64. {
  65. result = Strings.Format(aVal, longFormat);
  66. }
  67. catch (Exception expr_56)
  68. {
  69. ProjectData.SetProjectError(expr_56);
  70. ProjectData.ClearProjectError();
  71. }
  72. }
  73. var value = Math.Abs(aVal);
  74. if (value > 99999999)
  75. {
  76. result = Strings.Format(aVal / 100000000.0, "0.0") + LanguageManager.FindResource(LanguageConst.Currency_Unit_ThousandMillion);
  77. }
  78. else if (value > 9999999)
  79. {
  80. result = Strings.Format(aVal / 10000000, "0.0") + LanguageManager.FindResource(LanguageConst.Currency_Unit_TenMillion);
  81. }
  82. else if (value > 99999)
  83. {
  84. result = Strings.Format(aVal / 10000, "0.0") + LanguageManager.FindResource(LanguageConst.Currency_Unit_TenThousand);
  85. }
  86. return result;
  87. }
  88. //public static string GetAbbreviatedValue(double aVal, string longFormat)
  89. //{
  90. // string result = Strings.Format(aVal, "0.00");
  91. // bool flag = Operators.CompareString(longFormat, "", false) != 0;
  92. // if (flag)
  93. // {
  94. // try
  95. // {
  96. // result = Strings.Format(aVal, longFormat);
  97. // }
  98. // catch (Exception expr_56)
  99. // {
  100. // ProjectData.SetProjectError(expr_56);
  101. // ProjectData.ClearProjectError();
  102. // }
  103. // }
  104. // flag = (Math.Abs(aVal) > 900000000000.0);
  105. // if (flag)
  106. // {
  107. // result = Strings.Format(aVal / 1000000000000.0, "0.0") + "T";
  108. // }
  109. // else
  110. // {
  111. // flag = (Math.Abs(aVal) > 900000000.0);
  112. // if (flag)
  113. // {
  114. // result = Strings.Format(aVal / 1000000000.0, "0.0") + "B";
  115. // }
  116. // else
  117. // {
  118. // flag = (Math.Abs(aVal) > 900000.0);
  119. // if (flag)
  120. // {
  121. // result = Strings.Format(aVal / 1000000.0, "0.0") + "M";
  122. // }
  123. // else
  124. // {
  125. // flag = (Math.Abs(aVal) > 900.0);
  126. // if (flag)
  127. // {
  128. // result = Strings.Format(aVal / 1000.0, "0.0") + "K";
  129. // }
  130. // }
  131. // }
  132. // }
  133. // return result;
  134. //}
  135. }
  136. }