Created
February 11, 2019 16:01
-
-
Save JacobBennett/02bc2ef0cc372e47536134d0b96c91ea to your computer and use it in GitHub Desktop.
Handle the conversion of a float (decimal) to an integer.
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 toInt($float) | |
{ | |
// handle blank values | |
if (blank($float)) { | |
return 0; | |
} | |
// separate at decimal | |
$pieces = explode('.', $float); | |
// take first group as dollar amount | |
$dollars = $pieces[0]; | |
// take second group (if exists) as cent amount | |
$cents = (isset($pieces[1]) ? $pieces[1] : '00'); | |
// add two digits to end of cents | |
$cents = $cents . '00'; | |
// take first two values of cents group | |
$cents = substr($cents, 0, 2); | |
// concat group one and group two | |
return (int) $dollars . $cents; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment