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

# 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
• 2
• 2
4 Solutions

Commented:
You can use round() (http://www.php.net/round):
``````round(5.232332, 1);
``````
0

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;
``````
0

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

Commented:

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

ProfessorCommented:
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.