Skip to content

Instantly share code, notes, and snippets.

@hjr3
Last active December 13, 2015 23:09
Show Gist options
  • Save hjr3/4989386 to your computer and use it in GitHub Desktop.
Save hjr3/4989386 to your computer and use it in GitHub Desktop.
Integer precision loss when converting float to integer.
<?php
$outstandingBalance = -34.05;
$pennies = abs($outstandingBalance) * 100;
var_dump((int) $pennies); // => int(3404)
@hjr3
Copy link
Author

hjr3 commented Feb 19, 2013

The fix is to round before casting to an int.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment