NumberUtil.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public class NumberUtil
  6. {
  7. public static float angleToRadian(float angle)
  8. {
  9. return angle/180*Mathf.PI;
  10. }
  11. public static float radianToAngle(float radian)
  12. {
  13. return radian/Mathf.PI*180;
  14. }
  15. public static float getRadianByATan(float targetX, float targetY, float originX, float originY)
  16. {
  17. float dx = targetX-originX;
  18. float dy = targetY-originY;
  19. return Mathf.Atan2(dy, dx);
  20. }
  21. public static float forceBetween(float origin, float min, float max)
  22. {
  23. if(origin < min) return min;
  24. if(origin > max) return max;
  25. return origin;
  26. }
  27. public static int forceBetween(int origin, int min, int max)
  28. {
  29. if(origin < min) return min;
  30. if(origin > max) return max;
  31. return origin;
  32. }
  33. public static float getCloseToTargetAngle(float origin, float target, float step)
  34. {
  35. origin = corverAngleBetween(origin, -180f, 180f);
  36. target = corverAngleBetween(target, -180f, 180f);
  37. float deltaAngle = Mathf.Abs(target - origin);
  38. if(deltaAngle<=step || deltaAngle >= (360f-step))
  39. {
  40. origin = target;
  41. }
  42. else if(target > origin)
  43. {
  44. if(target-origin > 180f)
  45. {
  46. origin -= step;
  47. }
  48. else
  49. {
  50. origin += step;
  51. }
  52. }
  53. else
  54. {
  55. if(origin-target > 180f)
  56. {
  57. origin += step;
  58. }
  59. else
  60. {
  61. origin -= step;
  62. }
  63. }
  64. return origin;
  65. }
  66. public static float corverAngleBetween(float angle, float min, float max)
  67. {
  68. if(angle < min)
  69. {
  70. while(angle < min)
  71. {
  72. angle += 360f;
  73. }
  74. }
  75. else if(angle > max)
  76. {
  77. while(angle > max)
  78. {
  79. angle -= 360f;
  80. }
  81. }
  82. return angle;
  83. }
  84. public static float distanceVector2(Vector2 origin, Vector2 target)
  85. {
  86. return Vector2.Distance(origin, target);
  87. }
  88. public static float distanceVector3(Vector3 origin, Vector3 target, bool ignoreY=false)
  89. {
  90. if(ignoreY)
  91. origin.y = target.y = 0;
  92. return Vector3.Distance(origin, target);
  93. }
  94. public static string getTimeString(float seconds, bool enableHour=true)
  95. {
  96. bool isNegative = false;
  97. if(seconds < 0)
  98. {
  99. isNegative = true;
  100. seconds = -seconds;
  101. }
  102. int sec = (int)seconds;
  103. int h = enableHour?sec/(60*60):0;
  104. int tempSec = enableHour?sec%(60*60):sec;
  105. int m = tempSec/60;
  106. tempSec = tempSec%60;
  107. int s = tempSec;
  108. string hStr = enableHour?("00"+h):"";
  109. string mStr = "00"+m;
  110. string sStr = "00"+s;
  111. string str = "";
  112. if(enableHour)
  113. str = hStr.Substring(hStr.Length-2, 2)+":"+mStr.Substring(mStr.Length-2, 2)+":"+sStr.Substring(sStr.Length-2, 2);
  114. else
  115. str = mStr.Substring(mStr.Length-2, 2)+":"+sStr.Substring(sStr.Length-2, 2);
  116. if(isNegative)
  117. str = "-"+str;
  118. return str;
  119. }
  120. public static string GetPercentText(double value, int digits)
  121. {
  122. string str = Math.Round((double)(value * 100), digits).ToString();
  123. return str + "%";
  124. }
  125. public static string AddThousandSplit(int value)
  126. {
  127. List<string> list = new List<string>();
  128. do{
  129. int num = value%1000;
  130. value = value/1000;
  131. if(value > 0)
  132. list.Insert(0, StringUtil.FillZero(num, 3));
  133. else
  134. list.Insert(0, num.ToString());
  135. }
  136. while(value != 0);
  137. return StringUtil.Join(list, " ");
  138. }
  139. }