AnotherQuestion Posted September 4, 2009 Share Posted September 4, 2009 I would like to remove all characters after 2decimal places eg 34.0000 would become 34.00 32.9800 would become 32.98 and 7.7000 would become 7.70 cant find any reference to on how to do it. Quote Link to comment https://forums.phpfreaks.com/topic/173155-solved-remove-trailings-00/ Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 $haystack = '34.0000'; $point = strpos($haystack, '.'); //Search for decimal. $needle = substr($haystack, 0, $point + 3); // Only echo $needle; Quote Link to comment https://forums.phpfreaks.com/topic/173155-solved-remove-trailings-00/#findComment-912681 Share on other sites More sharing options...
Mark Baker Posted September 4, 2009 Share Posted September 4, 2009 number_format() Quote Link to comment https://forums.phpfreaks.com/topic/173155-solved-remove-trailings-00/#findComment-912682 Share on other sites More sharing options...
Psycho Posted September 4, 2009 Share Posted September 4, 2009 Yes, number_format() is what you want. $original = 12.3456789; $two_decimal = number_format($original, 2); //Output: 12.35 It will also have the effect of increasing the value to two decimals if less than two. In addition, it will round the value to two decimals if needed. Quote Link to comment https://forums.phpfreaks.com/topic/173155-solved-remove-trailings-00/#findComment-912701 Share on other sites More sharing options...
AnotherQuestion Posted September 4, 2009 Author Share Posted September 4, 2009 thanks bundyxc your method worked. final code used was <?php $InvTotal = $row_RSInvoiceHeader['Invoice_Total']; $point = strpos($InvTotal, '.'); $InvTotalSum = substr($InvTotal, 0, $point + 3); ?> <?php echo $InvTotalSum; ?> Quote Link to comment https://forums.phpfreaks.com/topic/173155-solved-remove-trailings-00/#findComment-912719 Share on other sites More sharing options...
Psycho Posted September 4, 2009 Share Posted September 4, 2009 thanks bundyxc your method worked. final code used was <?php $InvTotal = $row_RSInvoiceHeader['Invoice_Total']; $point = strpos($InvTotal, '.'); $InvTotalSum = substr($InvTotal, 0, $point + 3); ?> <?php echo $InvTotalSum; ?> Ok, you do realize that there is a built-in function within PHP to do exactly what you asked for which Mark Baker first suggested, right? But instead you decide to build a customer function for something PHP already does. Brilliant! You could replce those four lines of code with just this echo number_format($row_RSInvoiceHeader['Invoice_Total'], 2); Quote Link to comment https://forums.phpfreaks.com/topic/173155-solved-remove-trailings-00/#findComment-912730 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 I didn't even think of that function, but I recommend it more than a ghetto handmade function. Quote Link to comment https://forums.phpfreaks.com/topic/173155-solved-remove-trailings-00/#findComment-912733 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.