Omzy Posted October 7, 2009 Share Posted October 7, 2009 $price=250.00; $quantity=2; $total=$price*$quantity; echo $total; //outputs 500 I want it to output 500.00 Quote Link to comment https://forums.phpfreaks.com/topic/176867-solved-php-arithmetic-and-decimal-points/ Share on other sites More sharing options...
mikesta707 Posted October 7, 2009 Share Posted October 7, 2009 use number_format() $price=250.00; $quantity=2; $total=number_format($price*$quantity, 2); echo $total; //outputs 500.00 Quote Link to comment https://forums.phpfreaks.com/topic/176867-solved-php-arithmetic-and-decimal-points/#findComment-932569 Share on other sites More sharing options...
Omzy Posted October 7, 2009 Author Share Posted October 7, 2009 Nice one bro, that worked Quote Link to comment https://forums.phpfreaks.com/topic/176867-solved-php-arithmetic-and-decimal-points/#findComment-932571 Share on other sites More sharing options...
Omzy Posted October 7, 2009 Author Share Posted October 7, 2009 I'm now trying to add up values to get a subtotal: $subtotal=0; foreach($_SESSION as $value) { $total=number_format($value[2]*$value[3], 2); $subtotal=number_format($subtotal+$total, 2); } echo $subtotal; But this does not always display the correct value for $subtotal Quote Link to comment https://forums.phpfreaks.com/topic/176867-solved-php-arithmetic-and-decimal-points/#findComment-932660 Share on other sites More sharing options...
Omzy Posted October 7, 2009 Author Share Posted October 7, 2009 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/176867-solved-php-arithmetic-and-decimal-points/#findComment-932684 Share on other sites More sharing options...
Mchl Posted October 7, 2009 Share Posted October 7, 2009 Use number_format just before echo. Until then just do your math without worrying about decimals. $subtotal=0; foreach($_SESSION as $value) { $total=$value[2]*$value[3]; $subtotal=$subtotal+$total; } echo number_format($subtotal,2); Quote Link to comment https://forums.phpfreaks.com/topic/176867-solved-php-arithmetic-and-decimal-points/#findComment-932687 Share on other sites More sharing options...
Omzy Posted October 7, 2009 Author Share Posted October 7, 2009 Nice one bro, that worked Quote Link to comment https://forums.phpfreaks.com/topic/176867-solved-php-arithmetic-and-decimal-points/#findComment-932690 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.