MCPcopy Create free account
hub / github.com/Rello/analytics / normalizeNumberString

Method normalizeNumberString

lib/Service/ThresholdService.php:126–157  ·  view source on GitHub ↗
(string $str)

Source from the content-addressed store, hash-verified

124 }
125
126 private function normalizeNumberString(string $str): string {
127 $str = trim($str);
128 $hasComma = str_contains($str, ',');
129 $hasDot = str_contains($str, '.');
130
131 if ($hasComma && $hasDot) {
132 if (strrpos($str, ',') > strrpos($str, '.')) {
133 // comma as decimal separator
134 $str = str_replace('.', '', $str);
135 $str = str_replace(',', '.', $str);
136 } else {
137 // dot as decimal separator
138 $str = str_replace(',', '', $str);
139 }
140 } elseif ($hasComma) {
141 $idx = strrpos($str, ',');
142 $digits = strlen($str) - $idx - 1;
143 if ($digits <= 2) {
144 $str = str_replace(',', '.', $str);
145 } else {
146 $str = str_replace(',', '', $str);
147 }
148 } elseif ($hasDot) {
149 $idx = strrpos($str, '.');
150 $digits = strlen($str) - $idx - 1;
151 if ($digits > 2) {
152 $str = str_replace('.', '', $str);
153 }
154 }
155
156 return str_replace(' ', '', $str);
157 }
158
159 /**
160 * Compare two values and return comparison result similar to spaceship operator

Callers 1

compareValuesMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected