Last active
May 22, 2023 05:09
-
-
Save colynb/fde7f6c0fdae9170a8f1dcfd65935f85 to your computer and use it in GitHub Desktop.
PHP - convert any dollar/cents amount into just cents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function to_pennies($value) | |
{ | |
return intval( | |
strval(floatval( | |
preg_replace("/[^0-9.]/", "", $value) | |
) * 100) | |
); | |
} | |
echo to_pennies("1"); // 100 | |
echo to_pennies("12"); // 1200 | |
echo to_pennies("123"); // 12300 | |
echo to_pennies("123.45"); // 12345 | |
echo to_pennies("$123.45"); // 12345 | |
echo to_pennies("$12,345.67"); // 1234567 | |
echo to_pennies("$123,456.78"); // 12345678 | |
echo to_pennies("$1,234,567.89"); // 123456789 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! Didn't know anyone was using this. Thanks for the tip!