Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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