Skip to content

Instantly share code, notes, and snippets.

@desigens
Created October 22, 2012 13:22
Show Gist options
  • Save desigens/3931483 to your computer and use it in GitHub Desktop.
Save desigens/3931483 to your computer and use it in GitHub Desktop.
Hack for match with PHP's round()
<?php
$rate = (float) 32.0115;
echo '<script>rate = ' . $rate . ';</script>';
?>
<script>
function check(i, result)
{
var r = rate + 0.000000000001; // Hack for match with PHP's round()
var jsresult = i * r;
var fresult = jsresult.toFixed(2);
if(fresult != result)
{
// console.log(result,jsresult,fresult);
alert(result,jsresult,fresult);
}
}
</script>
<?php
for ($i = 0; $i < 10000; $i++)
{
$result = round($i * $rate, 2);
echo $i . ' = ' . $result . '<br>';
echo '<script>check('.$i.', '.$result.');</script>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment