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
239 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now