• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

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
0
mgtm3
Asked:
mgtm3
  • 2
  • 2
4 Solutions
 
glcumminsCommented:
You can use round() (http://www.php.net/round):
round(5.232332, 1);

Open in new window

0
 
richdiesalProfessorCommented:
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
 
glcumminsCommented:
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
 
bansidharCommented:

$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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now