@@ 83-100 (lines=18) @@ | ||
80 | * @param int $precision Number of number after decimal point. |
|
81 | * @return string A string number formated as 1K-1.5K |
|
82 | */ |
|
83 | public static function formatThousand($value, $precision): string |
|
84 | { |
|
85 | //$format = 1000; // If you dont wnat 0.9k but straight 1k for 999 |
|
86 | // $treshold2 = 999; // If you want 0.9k for 999 |
|
87 | $range = [999, 999999]; |
|
88 | $suffix = 'K'; |
|
89 | // Check if value is a valid integer and does not start with 0, and force user to pass value as string |
|
90 | self::validateNumber($value); |
|
91 | self::validateRange($value, $range); |
|
92 | ||
93 | //if (isset(self::$thousand_format)) |
|
94 | // $clean_number = $value / self::$thousand_format; |
|
95 | $clean_number = $value / self::$format[$suffix]; |
|
96 | // Round and format number |
|
97 | $formated_number = number_format($clean_number,$precision); |
|
98 | ||
99 | return (preg_match('/\\d\.0$/', $formated_number)) ? rtrim(rtrim($formated_number,'0'), '.').$suffix : $formated_number.$suffix; |
|
100 | } |
|
101 | ||
102 | /** |
|
103 | * Convert 1,000,000 place to 1M |
|
@@ 108-123 (lines=16) @@ | ||
105 | * @param int $precision Number of number after decimal point. |
|
106 | * @return string A string number formated as 1M-1.5M |
|
107 | */ |
|
108 | public static function formatMillion($value, $precision): string |
|
109 | { |
|
110 | $range = [999999, 999999999]; |
|
111 | $suffix = 'M'; |
|
112 | // Check if value is a valid integer and does not start with 0, and force user to pass value as string |
|
113 | self::validateNumber($value); |
|
114 | self::validateRange($value, $range); |
|
115 | ||
116 | //if (isset(self::$million_format)) |
|
117 | // $clean_number = $value / self::$million_format; |
|
118 | $clean_number = $value / self::$formats[$suffix]; |
|
119 | // Round and format number |
|
120 | $formated_number = number_format($clean_number,$precision); |
|
121 | ||
122 | return (preg_match('/\\d\.0$/', $formated_number)) ? rtrim(rtrim($formated_number,'0'), '.').$suffix : $formated_number.$suffix; |
|
123 | } |
|
124 | ||
125 | /** |
|
126 | * Convert 1000,000,000 place to 1B |
|
@@ 131-146 (lines=16) @@ | ||
128 | * @param int $precision Number of number after decimal point. |
|
129 | * @return string A string number formated as 1B-1.5B |
|
130 | */ |
|
131 | public static function formatBillion($value, $precision): string |
|
132 | { |
|
133 | $range = [999999999, 999999999999]; |
|
134 | $suffix = 'B'; |
|
135 | // Check if value is a valid integer and does not start with 0, and force user to pass value as string |
|
136 | self::validateNumber($value); |
|
137 | self::validateRange($value, $range); |
|
138 | ||
139 | //if (isset(self::$billion_format)) |
|
140 | // $clean_number = $value / self::$billion_format; |
|
141 | $clean_number = $value / self::$formats[$suffix]; |
|
142 | // Round and format number |
|
143 | $formated_number = number_format($clean_number,$precision); |
|
144 | ||
145 | return (preg_match('/\\d\.0$/', $formated_number)) ? rtrim(rtrim($formated_number,'0'), '.').$suffix : $formated_number.$suffix; |
|
146 | } |
|
147 | ||
148 | /** |
|
149 | * Convert 1,000,000,000,000 place to 1T |
|
@@ 154-169 (lines=16) @@ | ||
151 | * @param int $precision Number of number after decimal point. |
|
152 | * @return string A string number formated as 1B-1.5B |
|
153 | */ |
|
154 | public static function formatTrillion($value, $precision): string |
|
155 | { |
|
156 | $range = [999999999999, 999999999999000]; |
|
157 | $suffix = 'T'; |
|
158 | // Check if value is a valid integer and does not start with 0, and force user to pass value as string |
|
159 | self::validateNumber($value); |
|
160 | self::validateRange($value, $range); |
|
161 | ||
162 | //if (isset(self::$trillion_format)) |
|
163 | // $clean_number = $value / self::$trillion_format; |
|
164 | $clean_number = $value / self::$formats[$suffix]; |
|
165 | // Round and format number |
|
166 | $formated_number = number_format($clean_number,$precision); |
|
167 | ||
168 | return (preg_match('/\\d\.0$/', $formated_number)) ? rtrim(rtrim($formated_number,'0'), '.').$suffix : $formated_number.$suffix; |
|
169 | } |
|
170 | ||
171 | ||
172 | /** |