how can i just take 1 number after the . ? example 5.232332 i want to get 5.2

how can i just take 1 number after the . ? example 5.232332 i want to get 5.2
mgtm3Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
glcumminsConnect With a Mentor Commented:
You can use round() (http://www.php.net/round):
round(5.232332, 1);

Open in new window

0
 
richdiesalConnect With a Mentor ProfessorCommented:
That's only true if you want to round... if you want THE number after the . (instead of rounding up if the number if above .5, or rounding down if it's below), you could use this code snippet.
$var = 5.232332;
$new = (FLOOR($var*10))/10;

Open in new window

0
 
glcumminsConnect With a Mentor Commented:
If you are treating the number as a string rather than as a number, here is a more efficient way:

substr(5.232332, 0, 3);

No extra math is needed using this method.
0
 
bansidharConnect With a Mentor Commented:

$var = 5.232332;
$var = preg_replace('/(\d*\.\d)\d*/','$1',$var);

Open in new window

0
 
richdiesalProfessorCommented:
But it should be noted that glcummins last method only works if you are trying to shorten this specific number.  If the number has more digits before the decimal, it will cut them off.

So, I guess to summarize these methods:

Method 1:
5.232332 shortens to 5.2
Method 2:
5.232332 shortens to 5.2
Method 3:
5.232332 shortens to "5.2"

Method 1:
16.5812 shortens to 16.6
Method 2:
16.5812 shortens to 16.5
Method 3:
16.5812 shortens to "16."
0
All Courses

From novice to tech pro — start learning today.