shivani.shm Posted December 4, 2007 Share Posted December 4, 2007 I have a number as $num = '10.667' and i wnt the output as 10.66 . I tried using sprintf it gives me the output as 10.67 can anyone tell me how to do it.. thanks in advance..... Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 4, 2007 Share Posted December 4, 2007 you want to use a floor function it looks like as you want to round down it appears however floor only returns whole integers, so what you will need to do is somethign a bit fancier try <?php function dec_floor($int){ $tmp = explode(".",$int); $ret = $tmp[0].".".substr($temp[2],0,2); return $ret; } ?> Quote Link to comment Share on other sites More sharing options...
shivani.shm Posted December 4, 2007 Author Share Posted December 4, 2007 thanks your code is wrking ... but is this the only way... is there any build in function which will directly output me 10.66 if i give 10.667 as the input... i m not interested in rounding the number just wnt it upto two decimal places.... thanks in advance.... Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 4, 2007 Share Posted December 4, 2007 if sprintf or number_format (http://us3.php.net/manual/en/function.number-format.php) don't do it then yes this is the way. It isn't going to hinder your speed and will always return a number with its full left side of the decimal with the right side stripped to 2 places (if its less than 2 it shows the . and what is there if nothign just a point) Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 4, 2007 Share Posted December 4, 2007 try this <?php $intNum = 10.667; echo ((int) ($intNum*100))/100; ?> Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 <?php $num = '10.667'; $formatted = sprintf("%01.5s", $num); die("Formatted: $formatted"); PhREEEk Quote Link to comment Share on other sites More sharing options...
dg Posted December 4, 2007 Share Posted December 4, 2007 try with this number .... it's not showing properly ........ $num = '129.99889'; Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 He didn't say the number changes format, and only gave one example format. The sprintf provided returns 5 characters, including the decimal point. I would hope the OP could easily modify the code to accomodate any number of characters to the left of the decimal... PhREEEk Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 4, 2007 Share Posted December 4, 2007 food for thought <?php $intNum = 129.3333; function number_cut($intNum,$intCut) { return ((int) ($intNum*('1'.str_repeat('0',$intCut))))/('1'.str_repeat('0',$intCut)); } echo number_cut($intNum,1)."<BR>"; echo number_cut($intNum,2)."<BR>"; echo number_cut($intNum,3); ?> Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 His starting number is a string, not a float, and he asked for a built-in function within PHP... are we providing the solutions or just trying to impress the gallery? PhREEEk Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 4, 2007 Share Posted December 4, 2007 well in PHP that would not matter as it type juggles, so you can specify it as a string the result would be the same. impress the gallery!! not at all Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 4, 2007 Share Posted December 4, 2007 as you can see there are a ton of ways to do it I did it as a string The second version was as a nubmer and now again as a string The madness will never stop! Quote Link to comment 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.