NumberUtil.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using UnityEngine;
  3. namespace GFGGame
  4. {
  5. public class NumberUtil
  6. {
  7. public static string GetChiniseNumberText(int num)
  8. {
  9. int numInUnit = 0;
  10. int numInTen = 0;
  11. string strInUnit = "";
  12. string strInTen = "";
  13. if (num == 0)
  14. {
  15. return "零";
  16. }
  17. if (num <= 10)
  18. {
  19. return GameConst.CHINESE_NUMBER[num - 1];
  20. }
  21. else if (num < 20)
  22. {
  23. numInUnit = num - 10;
  24. strInUnit = GameConst.CHINESE_NUMBER[numInUnit - 1];
  25. return "十" + strInUnit;
  26. }
  27. else if (num < 100)
  28. {
  29. numInTen = Mathf.FloorToInt(num / 10);
  30. strInTen = GameConst.CHINESE_NUMBER[numInTen - 1];
  31. numInUnit = num % 10;
  32. if (numInUnit == 0)
  33. {
  34. return strInTen + "十";
  35. }
  36. else
  37. {
  38. strInUnit = GameConst.CHINESE_NUMBER[numInUnit - 1];
  39. return strInTen + "十" + strInUnit;
  40. }
  41. }
  42. return "未知";
  43. }
  44. public static string GetOldChiniseNumberText(int num)
  45. {
  46. int numInUnit = 0;
  47. int numInTen = 0;
  48. string strInUnit = "";
  49. string strInTen = "";
  50. if (num == 0)
  51. {
  52. return "零";
  53. }
  54. if (num <= 10)
  55. {
  56. return GameConst.CHINESE_NUMBER_OLD[num - 1];
  57. }
  58. else if (num < 20)
  59. {
  60. numInUnit = num - 10;
  61. strInUnit = GameConst.CHINESE_NUMBER_OLD[numInUnit - 1];
  62. return "拾" + strInUnit;
  63. }
  64. else if (num < 100)
  65. {
  66. numInTen = Mathf.FloorToInt(num / 10);
  67. strInTen = GameConst.CHINESE_NUMBER_OLD[numInTen - 1];
  68. numInUnit = num % 10;
  69. if (numInUnit == 0)
  70. {
  71. return strInTen + "拾";
  72. }
  73. else
  74. {
  75. strInUnit = GameConst.CHINESE_NUMBER_OLD[numInUnit - 1];
  76. return strInTen + "拾" + strInUnit;
  77. }
  78. }
  79. return "未知";
  80. }
  81. public static string GetChiniseNumberWeekText(int num)
  82. {
  83. if (num >= 0 && num < GameConst.CHINESE_NUMBER.Length)
  84. {
  85. return GameConst.CHINESE_WEEK_NUMBER[num];
  86. }
  87. return "未知";
  88. }
  89. public static string ChangeNumberUnit(int num, int unit = 10000)
  90. {
  91. int count = num / unit;
  92. if (count > 0)
  93. {
  94. return string.Format("{0}万", count);
  95. }
  96. return num.ToString();
  97. }
  98. /// <summary>
  99. /// 计算折扣-- 一位小数向上取整
  100. /// </summary>
  101. /// <param name="originalPrice">原始的价格</param>
  102. /// <param name="discountedPrice">现在的价格</param>
  103. /// <returns></returns>
  104. public static string CalculateDiscount(double originalPrice, double discountedPrice)
  105. {
  106. double discountRate = discountedPrice / originalPrice;
  107. double doubleNum = Math.Ceiling(discountRate * 100) / 10;
  108. var roundedNumStr = doubleNum.ToString("0.0");
  109. return roundedNumStr;
  110. }
  111. }
  112. }