Maurice79 Posted February 19, 2009 Share Posted February 19, 2009 I've a made a sum which calculate the procent of a value. To bad it is mostly a long result like 67.32857835 and i'm wondering if there is a code that remove all numbers or characters after the 5th. This means the above result would look like 76.32 and the word 'phpfreaks' would be 'phpfr'. Quote Link to comment Share on other sites More sharing options...
jackpf Posted February 19, 2009 Share Posted February 19, 2009 <?php function str_trim($str) { echo substr($str, 0, 5); } ?> Tbh, a google search would have given you what you need. Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted February 19, 2009 Share Posted February 19, 2009 I've a made a sum which calculate the procent of a value. To bad it is mostly a long result like 67.32857835 and i'm wondering if there is a code that remove all numbers or characters after the 5th. This means the above result would look like 76.32 and the word 'phpfreaks' would be 'phpfr'. if you're using numbers you can round that number using round() so round(67.328578235, 2); would give you 67.33 Quote Link to comment Share on other sites More sharing options...
Maurice79 Posted February 19, 2009 Author Share Posted February 19, 2009 Thanks both for the quik reply! BTW i used google before making this topic, google is the first who i ask for help but i just didn't find it this time or better said i was not searhing for the correct words. Honestly said i'm not a hero with php actualy just a noob and need a litle bit more help if it is possible. Currenlty i have this. $percentage = 100 / (($result->ban_length * 60) / ($date_and_ban - $now)); result is 76.123456789 Do i need to change it with this? $percentage = round(100 / (($result->ban_length * 60) / ($date_and_ban - $now)), 2); result will be 76.12 And i have this. $player = $result->player_nick; result is "the name of the player" Actualy i don't know how to work with the below code. $player = $result->player_nick; function str_trim($str) { echo substr($str, 0, 10); } result is "the name of t" Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted February 19, 2009 Share Posted February 19, 2009 Thanks both for the quik reply! <?php // the code you posted for this should work ok but I like to use extra parentheses to make things absolutely sure like this: $percentage = round( ( 100 / ( ( $result->ban_length * 60 ) / ($date_and_ban - $now ) ) ), 2); //and for this part: $player = $result->player_nick; $MaxLength = 10; //this is the number you change to set the max length of your string echo substr($player, 0, $MaxLength); // this will echo this: "The name o" $MaxLength = 19; echo substr($player, 0, $MaxLength); // this will echo this: "The name of the pla" hope that helps Quote Link to comment Share on other sites More sharing options...
Maurice79 Posted February 19, 2009 Author Share Posted February 19, 2009 Thank you very much 5kyy8lu3, now i understand also how the 2nd code works, also thanks for the detailed examples! Quote Link to comment 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.