phpbeginner Posted September 19, 2008 Share Posted September 19, 2008 Trying to round math to 4 decimal places but cuts of if it ends in 0's. For example want to display 1.9800 but displays as 1.98 Here is my round code $subtotal = ROUND ('1.0000' * ($row[2] + $row[3] + $row[4]), 4) ; Quote Link to comment https://forums.phpfreaks.com/topic/124953-solved-round-to-4-decimal-help/ Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 you want sprintf() <pre><?php $num = 12345.12; echo sprintf('%.4f', $num ); ?></pre> Quote Link to comment https://forums.phpfreaks.com/topic/124953-solved-round-to-4-decimal-help/#findComment-645639 Share on other sites More sharing options...
F1Fan Posted September 19, 2008 Share Posted September 19, 2008 What's wrong with number_format()? Quote Link to comment https://forums.phpfreaks.com/topic/124953-solved-round-to-4-decimal-help/#findComment-645641 Share on other sites More sharing options...
.josh Posted September 19, 2008 Share Posted September 19, 2008 what's wrong with sprintf()? Quote Link to comment https://forums.phpfreaks.com/topic/124953-solved-round-to-4-decimal-help/#findComment-645643 Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 Beat me to it Quote Link to comment https://forums.phpfreaks.com/topic/124953-solved-round-to-4-decimal-help/#findComment-645645 Share on other sites More sharing options...
F1Fan Posted September 19, 2008 Share Posted September 19, 2008 I guess it's personal preference. However, sprintf is for strings and number_format is for numbers, so to me it makes more sense to "format a number" using the function that is designed to format numbers, rather than strings. Again, maybe it's just me. There's nothing "wrong" with sprintf, especially if you're used to using it more. I almost never use sprintf, because I mostly format numbers, so number_format is what I know. Quote Link to comment https://forums.phpfreaks.com/topic/124953-solved-round-to-4-decimal-help/#findComment-645646 Share on other sites More sharing options...
phpbeginner Posted September 19, 2008 Author Share Posted September 19, 2008 You lost me with sprintf.......so can anyone help me or know why my code is not working? Thanks In Advance. $subtotal = ROUND ('1.0000' * ($row[2] + $row[3] + $row[4]), 4) ; Quote Link to comment https://forums.phpfreaks.com/topic/124953-solved-round-to-4-decimal-help/#findComment-645651 Share on other sites More sharing options...
F1Fan Posted September 19, 2008 Share Posted September 19, 2008 This will give you what you want: <?php $subtotal = number_format(($row[2] + $row[3] + $row[4]), 4, '.', ''); ?> Quote Link to comment https://forums.phpfreaks.com/topic/124953-solved-round-to-4-decimal-help/#findComment-645652 Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 You lost me with sprintf.......so can anyone help me or know why my code is not working? Thanks In Advance. We did help you. I even gave you a working example.... I'm not going to code for you. Quote Link to comment https://forums.phpfreaks.com/topic/124953-solved-round-to-4-decimal-help/#findComment-645653 Share on other sites More sharing options...
Mchl Posted September 19, 2008 Share Posted September 19, 2008 It's not working, because round() function returns a float number. Whenever you echo a float, any zeroes at the end are omitted. To echo float with exactly 4 places after a decimal separator you need to use one of the functions that will convert it to string. This can be either $n = sprintf('%.4f', $yourNumber ); or $n = number_format($yourNumber,4); Quote Link to comment https://forums.phpfreaks.com/topic/124953-solved-round-to-4-decimal-help/#findComment-645654 Share on other sites More sharing options...
phpbeginner Posted September 19, 2008 Author Share Posted September 19, 2008 Thanks so much for your help! Worked fine. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/124953-solved-round-to-4-decimal-help/#findComment-645655 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.