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
238 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

15 Experts available now in Live!

Get 1:1 Help Now