Skip to content

Commit

Permalink
Added support for thousands (string with 3 decimals).
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvandertuijn committed Mar 31, 2022
1 parent 3613497 commit c4dd739
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ number_format_to_decimal('1000,99'); // 1000.99
number_format_to_decimal('1000.99'); // 1000.99
number_format_to_decimal('1.000,99'); // 1000.99
number_format_to_decimal('1,000.99'); // 1000.99
number_format_to_decimal('10.000'); // 10000
number_format_to_decimal('100.000'); // 100000
number_format_to_decimal('10,000'); // 10000
number_format_to_decimal('100,000'); // 100000
number_format_to_decimal('1.000.000'); // 1000000
number_format_to_decimal('1.000.000,99'); // 1000000.99
number_format_to_decimal('1,000,000'); // 1000000
Expand Down
28 changes: 24 additions & 4 deletions src/number_format_to_decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,36 @@ function number_format_to_decimal(?string $value)
$countDotSeperators += substr_count($value, '.');
}

// If there is one separator, and it is a comma, we replace the comma with a period.
// If there is one separator, and it is a comma.

if ($countCommaSeperators == 1 && $countDotSeperators == 0) {
$decimal = str_replace(',', '.', $value);
$countDecimals = strlen(substr($value, strpos($value, ",") + 1));

// If there are 3 decimal places, we can assume that it is a thousand.

if ($countDecimals == 3) {
// Remove the comma.
$decimal = str_replace(',', '', $value);
} else {
// Replace the comma with a period.
$decimal = str_replace(',', '.', $value);
}
}

// If there's one separator, and it's a dot, we don't do anything.
// If there's one separator, and it's a dot.

elseif ($countCommaSeperators == 0 && $countDotSeperators == 1) {
$decimal = $value;
$countDecimals = strlen(substr($value, strpos($value, ".") + 1));

// If there are 3 decimal places, we can assume that it is a thousand.

if ($countDecimals == 3) {
// We remove the period.
$decimal = str_replace('.', '', $value);
} else {
// Do Noting
$decimal = $value;
}
}

// If there are two different separators.
Expand Down

0 comments on commit c4dd739

Please sign in to comment.