Jump to content

Formating the number


shivani.shm

Recommended Posts

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;
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/80072-formating-the-number/#findComment-405791
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/80072-formating-the-number/#findComment-405797
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/80072-formating-the-number/#findComment-405799
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/80072-formating-the-number/#findComment-405853
Share on other sites

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);

?>

Link to comment
https://forums.phpfreaks.com/topic/80072-formating-the-number/#findComment-405859
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.