Code Duplication    Length = 16-18 lines in 4 locations

src/ShortenNums.php 4 locations

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