|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Jun 2007
Posts: 266
|
rounding decimal points
Suppose a have a variable $num, it can be any values, like for example , 0.7849999999999999. What's the best way to round upwards it to maximum of 2 decimals i.e 0.7849999999999999 to 0.79?
i.e if i were looking for 3 decimal then it would have been 0.785. |
|
|
|
|
|
#2 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Dec 2005
Location: Cambridge, England
Posts: 781
|
Check out: http://php.net/manual/en/function.round.php
|
|
|
|
|
|
#3 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Jun 2007
Posts: 266
|
That seems to do the trick, I feel silly I even posted this
![]() |
|
|
|
|
|
#4 | |
|
SitePoint Member
Join Date: Nov 2009
Posts: 7
|
Quote:
![]() |
|
|
|
|
|
|
#5 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Jun 2007
Posts: 266
|
Another doubt. How do I go about limiting to 2 decimals without rounding?
I checked at the php manual, no specific function that I could see. My idea is to somehow use explode and substr in combination, but is there a simpler way? example -------- 1.1749999999999998 becomes 1.17 1.1767489999999999 becomes 1.17 1.1021999999999999 becomes 1.1 |
|
|
|
|
|
#6 |
|
SitePoint Member
Join Date: Nov 2009
Posts: 7
|
This should do it:
PHP Code:
There are a few ways you can do it though. |
|
|
|
|
|
#7 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Jun 2007
Posts: 266
|
I'll try it out, as for number_format - yes I considered it, but it adds a 0 along the end is the number if something like 1.1021999999999999.
|
|
|
|
|
|
#8 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Jun 2007
Posts: 266
|
Hi, which of the following will be faster and use the least resources?
PHP Code:
PHP Code:
I want to use the one that works best, of the two however, I have no clue how preg_replace works and I clearly understand the explode and substr combination. And in both the if $number is 1.1021999999999999 then the result is 1.10, but I don't want the 0 part as it does not have any value - in this respect I'd like it to be 1.1. Now it's more or less same as number_format($number, 2) which also returns 1.10 . Any suggestions? ![]() |
|
|
|
|
|
#9 | |
|
SitePoint Enthusiast
![]() Join Date: Dec 2008
Posts: 81
|
Quote:
Example: PHP Code:
|
|
|
|
|
|
|
#10 |
|
SitePoint Enthusiast
![]() Join Date: Dec 2008
Posts: 81
|
Sorry.. didn't take rounding into account:
PHP Code:
|
|
|
|
|
|
#11 |
|
Previously, SilverBulletUK.
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Apr 2008
Location: North-East, UK.
Posts: 2,917
|
|
|
|
|
|
|
#12 | |
|
SitePoint Enthusiast
![]() Join Date: Dec 2008
Posts: 81
|
Quote:
![]() Granted, I just realized that given the rounding, I could have simply did something like: PHP Code:
|
|
|
|
|
|
|
#13 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2008
Posts: 4,765
|
You could also use arithmetic + integer truncation
PHP Code:
PHP Code:
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 14:14.














Linear Mode
