Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 9, 2009, 00:53   #1
cssExp
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.
cssExp is offline   Reply With Quote
Old Nov 9, 2009, 00:59   #2
Rubble
SitePoint Guru
 
Rubble's Avatar
 
Join Date: Dec 2005
Location: Cambridge, England
Posts: 781
Check out: http://php.net/manual/en/function.round.php
Rubble is offline   Reply With Quote
Old Nov 9, 2009, 01:26   #3
cssExp
SitePoint Addict
 
Join Date: Jun 2007
Posts: 266
That seems to do the trick, I feel silly I even posted this
cssExp is offline   Reply With Quote
Old Nov 9, 2009, 01:40   #4
Snowblind
SitePoint Member
 
Snowblind's Avatar
 
Join Date: Nov 2009
Posts: 7
Quote:
That seems to do the trick, I feel silly I even posted this
The PHP manual is excellent. If you haven't I'd suggest you bookmark it
Snowblind is offline   Reply With Quote
Old Nov 9, 2009, 18:38   #5
cssExp
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
cssExp is offline   Reply With Quote
Old Nov 9, 2009, 19:02   #6
Snowblind
SitePoint Member
 
Snowblind's Avatar
 
Join Date: Nov 2009
Posts: 7
This should do it:
PHP Code:

<?php
$number
= 1234.50789;
$trunc = preg_replace(
    
'/^.*?[0]*([\d\,]+)(([\.][\d]{0,2})([\d]*))?.*?$/',
    
'$1$3',
    
$number
);
echo
$trunc;
?>
From php.net/manual/en/function.number-format.php

There are a few ways you can do it though.
Snowblind is offline   Reply With Quote
Old Nov 9, 2009, 20:38   #7
cssExp
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.

Quote:
Originally Posted by Snowblind View Post
This should do it:
PHP Code:

<?php
$number
= 1234.50789;
$trunc = preg_replace(
    
'/^.*?[0]*([\d\,]+)(([\.][\d]{0,2})([\d]*))?.*?$/',
    
'$1$3',
    
$number
);
echo
$trunc;
?>
From php.net/manual/en/function.number-format.php

There are a few ways you can do it though.
cssExp is offline   Reply With Quote
Old Nov 9, 2009, 21:00   #8
cssExp
SitePoint Addict
 
Join Date: Jun 2007
Posts: 266
Hi, which of the following will be faster and use the least resources?

PHP Code:
$number = 1.1749999999999998;
 
$number = preg_replace('/^.*?[0]*([\d\,]+)(([\.][\d]{0,2})([\d]*))?.*?$/','$1$3', $number);
echo "preg_replace result " . $number . "<br>"; // uses 1 function and 1 variable

PHP Code:
$number = 1.1749999999999998;
 
list($num, $dec) = explode(".", $number); $number = $num . "." . substr($dec, 0, 2);
echo "explode_substr result " . $number . "<br>"; // uses 2 functions and 3 variables

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?
cssExp is offline   Reply With Quote
Old Nov 9, 2009, 21:45   #9
nrg_alpha
SitePoint Enthusiast
 
nrg_alpha's Avatar
 
Join Date: Dec 2008
Posts: 81
Quote:
Originally Posted by cssExp View Post
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?
I wouldn't bother with regex or exploding and such.. given what you are asking, perhaps something along the lines of:

Example:
PHP Code:

$number = '1.1021999999999999';

$number = (float) sprintf('%.2f', $number);
echo
$number; // Output: 1.1
?
nrg_alpha is online now   Reply With Quote
Old Nov 9, 2009, 22:08   #10
nrg_alpha
SitePoint Enthusiast
 
nrg_alpha's Avatar
 
Join Date: Dec 2008
Posts: 81
Sorry.. didn't take rounding into account:

PHP Code:

$number = '123.1031999999999999';
echo
$number = (float) sprintf('%.2f', round($number, 2)); // Output: 123.1
nrg_alpha is online now   Reply With Quote
Old Nov 10, 2009, 02:59   #11
AnthonySterling
Previously, SilverBulletUK.
 
AnthonySterling's Avatar
 
Join Date: Apr 2008
Location: North-East, UK.
Posts: 2,917
Quote:
Originally Posted by nrg_alpha View Post
Sorry.. didn't take rounding into account:

PHP Code:

$number = '123.1031999999999999';
echo
$number = (float) sprintf('%.2f', round($number, 2)); // Output: 123.1
Finally some sense, although I do suspect it was RegEx which drew you here.
AnthonySterling is offline   Reply With Quote
Old Nov 10, 2009, 10:06   #12
nrg_alpha
SitePoint Enthusiast
 
nrg_alpha's Avatar
 
Join Date: Dec 2008
Posts: 81
Quote:
Originally Posted by AnthonySterling View Post
Finally some sense, although I do suspect it was RegEx which drew you here.
lol.. it get's pretty bad when you know of my love affair with regex
Granted, I just realized that given the rounding, I could have simply did something like:

PHP Code:

$number = '123.1031999999999999';
echo
$number = round($number, 2); // Output: 123.1
Which kind of made the previous sample obsolete (it works harder than it needs to). EDIT - Initially, I didn't use rounding, but when I realized rouding was desired, I simply added the rounding function to the rest of the solution, which wasn't even needed anymore. D'Oh!
nrg_alpha is online now   Reply With Quote
Old Nov 10, 2009, 15:30   #13
crmalibu
SitePoint Wizard
 
Join Date: Jul 2008
Posts: 4,765
You could also use arithmetic + integer truncation
PHP Code:

echo (int)($n * 100) / 100; 

If you might need a different amount of decimal places, you can use this instead of the constant 100
PHP Code:

pow(10, $decimal_places); 

crmalibu is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 14:14.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved