asmith Posted January 23, 2008 Share Posted January 23, 2008 hey guys i want to show number which have more than 2 decimal to a 2 decimal form but not rounding or floor like: 2.234 => 2.23 54.73453 => 54.73 1.1 = > 1.1 4.56 => 4.56 45.53218753 => 45.53 12362.534 => 12362.53 what is the simplest way to do such thing ? i've tried money_format , but it rounds the number and can't be run in windows, only on linux servers . i was thinking about treat the number as a string, explode it with " . " , then count the digits after "." . and substr it to 2 digits . but it is way too much coding for a simple purpose . any better idea ? thanks Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/ Share on other sites More sharing options...
ziv Posted January 23, 2008 Share Posted January 23, 2008 try this one: number_format Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/#findComment-446735 Share on other sites More sharing options...
Daniel0 Posted January 23, 2008 Share Posted January 23, 2008 What you are doing is rounding it. $num = round($num, 2); Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/#findComment-446736 Share on other sites More sharing options...
asmith Posted January 23, 2008 Author Share Posted January 23, 2008 What you are doing is rounding it. $num = round($num, 2); round(23.345 , 2) = > 23.35 what i want is 23.34 Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/#findComment-446737 Share on other sites More sharing options...
Daniel0 Posted January 23, 2008 Share Posted January 23, 2008 <?php echo preg_replace('/^(\d+\.\d{2})\d*$/', '\\1', '23.345'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/#findComment-446741 Share on other sites More sharing options...
ziv Posted January 23, 2008 Share Posted January 23, 2008 y to use round or worse - to use regex for this simple task, if you have the number_format function? <?php $pi = 3.14159265; echo number_format($pi, 2); Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/#findComment-446742 Share on other sites More sharing options...
Daniel0 Posted January 23, 2008 Share Posted January 23, 2008 Because they don't do the same? number_format('1000.568', 2); // 1,000.57 round('1000.568', 2); // 1000.57 Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/#findComment-446743 Share on other sites More sharing options...
ziv Posted January 23, 2008 Share Posted January 23, 2008 Because they don't do the same? number_format('1000.568', 2); // 1,000.57 round('1000.568', 2); // 1000.57 if you don't need the thousands comma, no problem: <?php echo number_format('1000.568', 2, '', '.'); // 1000.57 Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/#findComment-446744 Share on other sites More sharing options...
Daniel0 Posted January 23, 2008 Share Posted January 23, 2008 <?php class Timer { private $start_time; function __construct() { $this->start_time = microtime(); } public function getTime() { $start_time = explode(' ', $this->start_time); $stop_time = explode(' ', microtime()); $start_time = $start_time[0] + $start_time[1]; $stop_time = $stop_time[0] + $stop_time[1]; return $stop_time - $start_time; } } $float = 1168168.681681; $precision = 2; $operations = 100000; $timer = new Timer(); for($i = 0; $i < $operations; $i++) { $new_num = round($float, $precision); } $round_time = $timer->getTime(); echo "round: {$round_time}\n"; $timer = new Timer(); for($i = 0; $i < $operations; $i++) { $new_num = number_format($float, $precision, '.', ''); } $number_format_time = $timer->getTime(); echo "number_format: {$number_format_time}\n"; ?> Output: round: 0.2045259475708 number_format: 0.69444394111633 round() is around 3.4 times faster. Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/#findComment-446747 Share on other sites More sharing options...
asmith Posted January 23, 2008 Author Share Posted January 23, 2008 thanks guys that was what i needed . btw , this is going to be intresting! Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/#findComment-446748 Share on other sites More sharing options...
asmith Posted January 23, 2008 Author Share Posted January 23, 2008 lol was wrong didnt work : number_format('1234.345', 2, '', '.') will echo 1.23435 way wrong ! number_format('1234.345', 2) echo 1,234.35 !!! it rounds too !!! Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/#findComment-446750 Share on other sites More sharing options...
Daniel0 Posted January 23, 2008 Share Posted January 23, 2008 Use the regex I posted: http://www.phpfreaks.com/forums/index.php/topic,178512.msg794517.html#msg794517 Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/#findComment-446753 Share on other sites More sharing options...
rajivgonsalves Posted January 23, 2008 Share Posted January 23, 2008 its simple math... here you go <?php $intNumber = "2.234"; echo ((int) ($intNumber*100))/100; ?> hope its helpful Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/#findComment-446756 Share on other sites More sharing options...
asmith Posted January 23, 2008 Author Share Posted January 23, 2008 lol yes rajiv! thanks!! it is simple math !! lol Quote Link to comment https://forums.phpfreaks.com/topic/87344-solved-show-2-digit-of-decimal/#findComment-446761 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.