Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • 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
 
richdiesalCommented:
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
 
richdiesalCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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