I have column named Profit/Loss(USD) , this column contains numbers which are defined by a set of calculations and the variable for this column. The problem is that some values come out like 7.0E-6 . How do i adjust all to 2 decimal places? all answers appreciated!
Use the in built function number_format like so:
number_format($number, 2);
If you don't want to add thousand seperators, you can simply skip it:
number_format($number, 2, '.', '');
// ^ No thousand seperator
you can use the function number_format to format your number
$formatted_number = number_format($number, 2);
You can use the printf family of functions, for example:
echo '<td>'.sprintf('%.2f', $number).'</td>';
The documentation lists all the formatting codes you can use.
Related
I have a code where I got some numbers like this:
92.682926829268
I'd like to cut them like this:
92.68
This is my code:
<td><?php if (($row['TotalMatch']) > 10){ echo ($row['OK_05'] / $row['TotalMatch']) * 100; } ?></td>
I tried with floor and round but I get that example I showed at the beginning of post ( 92.682926829268 instead of 92.68 )
Thanks for your attention
Regards!
EDIT Could you give me an example with my code? Thanks
Use sprintf() to format the number.
echo sprintf("%.2f", 92.682926829268);
Example:
https://3v4l.org/U87T9
The expression you're trying to format is this:
($row['OK_05'] / $row['TotalMatch']) * 100
So whichever function you decide to use needs to go around that expression.
As to which function to use, you need to select one that returns a string, not a float.
If you use round, and your expression returns a float that rounds to a number with two zeros after the decimal point, the trailing zeros will not be displayed in the result. For example, echo round(92.0006829268, 2) will display 92, not 92.00. So don't use round if you need to be sure that two decimal places are always displayed. round is a math function, not a formatting function.
floor is really not useful at all here, as it returns a number with no decimal places.
A simple way is to use sprintf as shown in some of the other answers.
echo sprintf("%.2f", ($row['OK_05'] / $row['TotalMatch']) * 100);
The first argument to sprintf is "%.2f", which is a format string indicating that the second argument should be displayed as a float with two decimal places. The second argument is your expression.
Using bcdiv as suggested in the other answer will also work, but it works a little differently that sprintf and will produce a slightly different result in some cases.
sprintf will round to the number of decimal places specified, so for example
echo sprintf("%.2f", 926.89 / 10); // outputs 92.69
and bcdiv will truncate instead, so
echo bcdiv(926.89, 10, 2); // outputs 92.68
Whichever one of those works for you, do that.
You can use the round function
$var = 92.682926829268;
$var = round($var, 2)
Or use sprintf (%.2f cuts the number)
$var = sprintf("%.2f", $var);
Try using sprintf like below:
<?php
$mynumber = 98.343434;
echo sprintf('%.2f', $mynumber); // this will output 98.34
You could use bcdiv()
bcdiv($row['OK_05'], ($row['TotalMatch'] * 100), 2);
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 have been trying all morning but how would I this number 1304583496 to look like this
13.04583496
and the same goes for this number
456604223 to 4.56604223
There are always 8 numbers to the right.
Divide it with 100000000, or use "pow" function:
$number / pow(10, 8);
You could also use the official number_format method after division to keep your ending zeroes and have your number displayed in a nice manner.
<?php
$num = 1304583496; //the number
echo number_format($num/100000000,8,"."," "); //number of decimals = 8, comma seperator is . and thousands seperator is a space here
?>
For more information on this function: http://www.w3schools.com/php/func_string_number_format.asp
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 11 months ago.
I need to convert numbers to have .00 after them, but only if the number is an integer, or it has just 1 number after the decimal point, like so:
1.4 = 1.40
45 = 45.00
34.77 = 34.77
What reg exp to use for this simple case?
You can also use printf or sprintf
printf("%01.2f", '34.77');
$formatted_num = sprintf("%01.2f", '34.77');
number_format($number, 2, '.', '');
Read more at PHP.net. You don't need to determine if a number is an integer or not -- as long as it's a number, it will be formatted to two decimal places.
If you'd like the thousands separator, change the last parameter to ','.
Check out PHP's built-in function number_format
You can pass it a variable and it'll format it to the correct decimal places
$number = 20;
if (is_int($number)) {
$number = number_format($number, 2, '.', '');
}
read number_format
number_format($number, 2, '.', '');
$num = 0.00638835;
$avg = sscanf($num,"%f")[0] /100;
echo sprintf("%.10f", $avg);
result 0.0000638835