Skip to content

Instantly share code, notes, and snippets.

@JacobBennett
Created February 11, 2019 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacobBennett/02bc2ef0cc372e47536134d0b96c91ea to your computer and use it in GitHub Desktop.
Save JacobBennett/02bc2ef0cc372e47536134d0b96c91ea to your computer and use it in GitHub Desktop.
Handle the conversion of a float (decimal) to an integer.
<?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