I have a problem in my Controller in Laravel 4. I try to round (or truncate) a decimal with 2 digits after comma, but it doesn't work.
I tried something like this :
round(888/100, 2);
Or :
floor(888*100)/100;
But I have 8.880000000000001 instead of 8.88.
Why?
(In this example I take this number but it can be an other number)
Try this after rounding :
number_format((float)$number, 2, '.', '');
try this
number_format($number, 2, '.', ',');
// 2 = decimal places | '.' = decimal seperator | ',' = thousand seperator
Related
I have a float: 9.174599999999995
Which when I run number_format(9.174599999999995, 2) I am getting 9.17. I believe I should be getting 9.18 as the 4 rounds up to 5 and then the 7 rounds to 8. Is this not right? How can I achieve the desired result of 9.18?
This is because number_format doesn't round :
http://php.net/manual/en/function.number-format.php
Take a look at round function :
http://php.net/manual/fr/function.round.php
Well, you assume wrong, the correct answer is 9.17, because the rounding is made based on just one digit after the rounding point.
Basically when you round 9.174599999999995 with 2 decimals, you round it based on 4, which is smaller than 5, so the number is rounded to 9.17
due to case you can use php round.
Examples:
$n=3.18925
$round=round($round, 1);
it will be displayed 3.2
$round=round($round, 2);
it will be displayed 3.19
For your number,
$number = 9.17499999999995 ;
echo $english_format_number = number_format($number, 2, '.', '');
9.17
$number = 9.17599999999995 ;
echo $english_format_number = number_format($number, 2, '.', '');
9.18
and by using number_format for 2 times you can achieve desire result
$number = 9.17499999999995 ;
echo $number = number_format($number, 3, '.', '');
echo "<br>";
echo $english_format_number = number_format($number, 2, '.', '');
RESULTS
9.175
9.18
I have this question:
How can round two decimals place to next multiple to ten? Follow this example:
$number = 120.37
// I would like some functions or trick to transform this into 120.40
Is it possible in php?
use this number format
number_format($number, 2, '.', '');
It will give output 120.40
try this
number_format(round($number,1), 2, '.', '');
or just
round($number,1);
if you not need it with 2 decimals
Try this
round($number * 10) / 10;
My number is 495 plain and simple.
I'm trying to format this number to have it display $4.95
what I've done is number_format($number, 2, '.', '')
but that outputs 495.00
I know it can't be this difficult. Anyone familiar with number_format?
I just need it to add a decimal two numbers from the right so the cents are always displayed. ex. 4.95, 12.58, 23.39... You get the idea.
Thank you.
If you're working with integers instead of floats, you'd first have to divide by 100.
$number = 495;
echo number_format($number/100, 2, '.', '')
Assuming your numbers are all whole numbers you can do the following:
$mynum = 495;
function formatMyNum($num) {
return $num/100;
}
Number format just displays floats using N number of decimals and the decimal/thousand separator characters you choose.
If you passed 4.95 to it, it would show fine so to do that just divide your $number by 100 before passing it to the function.
echo number_format($number/100, 2, '.', '');
I would like to make dots between my total value.
If i have 425000 i would like it to show as 425.000
Is there a function in php that implodes dots for numbers or how can i do this then?
Use number_format for this:
$number = 425000;
echo number_format( $number, 0, '', '.' );
The above example means: format the number, using 0 decimal places, an empty string as the decimal point (as we're not using decimal places anyway), and use . as the thousands separator.
Unless of course I misunderstood your intent, and you want to format the number as a number with 3 decimal places:
$number = 425000;
echo number_format( $number, 3, '.', '' );
The above example means: format the number, using 3 decimal places, . as the decimal point (default), and an empty string as the thousands separator (defaults to ,).
If the default thousands separator is good enough for you, you can just use 1:
$number = 425000;
echo number_format( $number, 3 );
1) Mind you: number_format accepts either 1, 2 or 4 parameters, not 3.
I guess you're looking for the number_format function.
It will actually be a decimal but that is not the main point. I will have a set of numbers like:
8976
8765
3454
3453
10198
What I am wanting to do is add a decimal 2 places from the right. So the first would be 89.76 and so forth.
Can't you just multiply each by 0.01?
$formatted = number_format($unformatted_number / 100, 2, '.', '');
2 - decimal places
'.' - decimal separator
'' - thousands separator
docs for the function are here.
try this
$number = 8976;
$number = (float)$number/100;
results:
89.76
You may have to do some checking to see how many digits the number is, i.e 89768 would be devided by 1000 and so on.
Comments are available,
//the string you need to split
$string = "123456";
// read from right 2 character
$rightNums = substr($string, -2, 2);
// maximum 100 character to the left defined now
$otherNums = substr($string, -4, 100);
// pront them just with . between
echo $otherNums.".".$rightNums; ?>
hope it help much.
Try with this
$tmpString = substr("8976", 0, -2);
$finalString = str_replace($tmpString, "." . $tmpString, "8976");
echo $finalString;