JudgementDay Posted February 7, 2012 Share Posted February 7, 2012 I want to compute 150 + 20.95 * 1.01 + .5 so I get '171.65'. echo (150 + 20.95 * 1.01 + .5); gives me '171.6595'. I tried echo number_format(150 + 20.95 * 1.01 + .5, 2);, but I got '171.66'. How can I go about getting '171.65'? Link to comment https://forums.phpfreaks.com/topic/256577-trying-to-get-a-point-two-decimal-outcome/ Share on other sites More sharing options...
JudgementDay Posted February 7, 2012 Author Share Posted February 7, 2012 ((int)((150 + 20.95 * 1.01 + .5) * 100)) / 100; Solved. Link to comment https://forums.phpfreaks.com/topic/256577-trying-to-get-a-point-two-decimal-outcome/#findComment-1315321 Share on other sites More sharing options...
JudgementDay Posted February 7, 2012 Author Share Posted February 7, 2012 I'm having a little trouble when using the solution with my own code. I am getting the error 'unexpected ;': $grandtotal = ((int)(($_SESSION['order']['contentvalue'] + $_SESSION['order']['deliverycost'] * 1.01 + $_SESSION['order']['paymentsurcharge'])) / 100; I can understand why ';' is unexpected? Link to comment https://forums.phpfreaks.com/topic/256577-trying-to-get-a-point-two-decimal-outcome/#findComment-1315328 Share on other sites More sharing options...
Pikachu2000 Posted February 7, 2012 Share Posted February 7, 2012 Count the parentheses. Link to comment https://forums.phpfreaks.com/topic/256577-trying-to-get-a-point-two-decimal-outcome/#findComment-1315333 Share on other sites More sharing options...
JudgementDay Posted February 7, 2012 Author Share Posted February 7, 2012 I don't know what that means. Anyway, the problem is now solved: $grandtotal = ((int)(($_SESSION['order']['contentvalue'] + $_SESSION['order']['deliverycost'] * 1.01 + $_SESSION['order']['paymentsurcharge']) * 100)) / 100; Link to comment https://forums.phpfreaks.com/topic/256577-trying-to-get-a-point-two-decimal-outcome/#findComment-1315334 Share on other sites More sharing options...
JudgementDay Posted February 7, 2012 Author Share Posted February 7, 2012 $grandtotal = ((int)((450 * 1.01) * 100)) / 100; gives me '454.5'. Is it possible to get '454.50' instead? Link to comment https://forums.phpfreaks.com/topic/256577-trying-to-get-a-point-two-decimal-outcome/#findComment-1315342 Share on other sites More sharing options...
kicken Posted February 7, 2012 Share Posted February 7, 2012 I don't know what that means. It means you count every ( you have and every ) you have. If the number doesn't match, then you have a problem. When you start dealing with lines like that, it often is better to fill in a few temporary variables and use those. $subTotal = $_SESSION['order']['contentvalue'] + $_SESSION['order']['deliverycost'] * 1.01; $total = $subTotal + $_SESSION['order']['paymentsurcharge']; $total = floor($total*100)/100; Much easier to read and follow; Is it possible to get '454.50' instead? number_format should take care of that for you. Link to comment https://forums.phpfreaks.com/topic/256577-trying-to-get-a-point-two-decimal-outcome/#findComment-1315370 Share on other sites More sharing options...
JudgementDay Posted February 7, 2012 Author Share Posted February 7, 2012 Hey bro, I find temporary variables cause spaghetti and crufts up my code. Hey, I found a solution to my problem. Its beyond my capacity to understand how it works, but all I know it does. I don't have time to learn how it works. I am badly behind schedule. $formula = ((int)((1 + 2 * 1.01 + 3) * 100)) / 100; $grandtotal = sprintf("%01.2f", $formula); Anyway, gotta run. Just came back to share the solution and mark as solved. Link to comment https://forums.phpfreaks.com/topic/256577-trying-to-get-a-point-two-decimal-outcome/#findComment-1315374 Share on other sites More sharing options...
JudgementDay Posted February 7, 2012 Author Share Posted February 7, 2012 P.S (INT) may not be neccessary Link to comment https://forums.phpfreaks.com/topic/256577-trying-to-get-a-point-two-decimal-outcome/#findComment-1315375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.