Guest User

Untitled

a guest
Aug 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. Unexpected round() return
  2. $line_item_price = 13.775;
  3.  
  4. echo round($line_item_price, 2, PHP_ROUND_HALF_DOWN);
  5.  
  6. $line_item_price = 13.775;
  7. echo floor($line_item_price * 100) / 100;
  8.  
  9. echo round($line_item_price, 2)
  10.  
  11. $num = 13.775;
  12. $tmp = intval($num*1000);
  13. $dec = $tmp % 10;
  14. if ($dec > 5) {
  15. $rounded = (1+intval($tmp/10))/100;
  16. } else {
  17. $rounded = intval($tmp/10)/100;
  18. }
  19. echo $rounded,"n";
  20.  
  21. $ php -r 'printf("%.40fn", 13.775);'
  22. 13.7750000000000003552713678800500929355621
  23.  
  24. $ php -r 'echo round(13.77500000000001, 2, PHP_ROUND_HALF_DOWN) . "n";'
  25. 13.77
  26.  
  27. function round_half_down ( $num, $digits ) {
  28. $mul = pow( 10, $digits );
  29. return ceil( $num * $mul - 0.5 ) / $mul;
  30. }
Add Comment
Please, Sign In to add comment