Jump to content

Maximum lenght


Maurice79

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/145842-maximum-lenght/#findComment-765728
Share on other sites

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"

 

Link to comment
https://forums.phpfreaks.com/topic/145842-maximum-lenght/#findComment-765747
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/145842-maximum-lenght/#findComment-765755
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.