Solved

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

Posted on 2008-10-01
5
243 Views
Last Modified: 2008-10-02
how can i just take 1 number after the . ? example 5.232332 i want to get 5.2
0
Comment
Question by:mgtm3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 24

Accepted Solution

by:
glcummins earned 250 total points
ID: 22616164
You can use round() (http://www.php.net/round):
round(5.232332, 1);

Open in new window

0
 
LVL 9

Assisted Solution

by:richdiesal
richdiesal earned 150 total points
ID: 22616226
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
 
LVL 24

Assisted Solution

by:glcummins
glcummins earned 250 total points
ID: 22616278
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
 
LVL 11

Assisted Solution

by:bansidhar
bansidhar earned 100 total points
ID: 22616367

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

Open in new window

0
 
LVL 9

Expert Comment

by:richdiesal
ID: 22616385
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 80
How do I allow for non standard CSV file 9 47
Last 4 numbers of a variable 9 24
Can't Find The Code 15 22
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question