How to show two Decimal point - php

I've an online test result page, my code is:
echo ($r1['om']/$r['maxmarks']*100)." %";
Result showing: 61.764705882353 %
How to setup 61.76 % instead?

use number_format()
echo number_format($r1['om']/$r['maxmarks']*100,2)

Related

How to cut decimal number using PHP

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

trouble with calculating percentage using PHP

I wanted to calculate the percentage using PHP. I tried the code given below but its gives me the return value in float. i don't know much in PHP so please fix this code.
current OUTPUT
66.666666666667%
Expected OUTPUT
66.66%
<?php
$up=4;
$down:2;
echo (($ups/($ups+$downs))*100).'%';
?>
Use number_format() to specify your decimals and separator.
<?php
$up=4;
$down:2;
$num = (($ups/($ups+$downs))*100).'%';
$formatted_num = number_format($num, 2, '.', '');
echo $formatted_num;
?>
You can do like this :
echo round(66.666666666667, 2); >> 66.66

Simple php .. not working...hmm Looks very simple

Please check the code below:
<?php
$d=2;
echo "the sum of the number is"."<sub>($d+1)</sub>";
?>
Its giving as output:
the sum of the number is <sub>(2+1)</sub>
Ideally I need the output to be "the sum of the number is <sub>3</sub>".
It works fine when we don't use the HTML tags <sub>...
How can I fix this?
try writing it as
<?php $d=2; echo "the sum of the number is"."<sub>".($d+1)."</sub>"; ?>
the quotes give it a string representation and thus not allowing you to perform addition.
Move the expression outside the quotes:
<?php
$d = 2;
echo "the sum of the number is <sub>" . ($d+1) . "</sub>";
?>

How to handle number_format output in php?

I am getting the following output correctly:
<?php echo number_format("12312.312","1"); // Correct Output 12312.3 ?>
but in the following case
<?php echo number_format("12312","1"); // Getting output 12312.0 but requires only 12312 ?>
So basically, I want to control my output i.e. it should add decimal place only if my decimal digit is greater than 0.
The second parameter for number_format() takes the number of decimals - so your example is the the expected result. Maybe you are intereseted in the round() function which allows to round to a certain precision?
You can try something like this
<?php
$number = 12312;
echo is_int($number) ? $number : number_format($number,"1");
?>
If you don't want the extra decimal place, use <?php echo number_format(12312, 0);?>
The "0" represents 0 decimal places
simple casting it to float, usign (float), works.
e.g.
$num_1 = (float)1.0;
$num_2 = (float)1.1;
echo $num_1;
echo $num_2;
output:
1
1.1

PHP calculation

I'm trying to output a value from xml this is what I do -
<?php
echo $responseTemp->Items->Item->CustomerReviews->AverageRating;
?>
This outputs 4.5, but when I change it to the code below it displays as 8. Why isn't it displaying as 9? Thanks.
<?php
echo $responseTemp->Items->Item->CustomerReviews->AverageRating*2;
?>
Try casting the value to a numerical value first.
$num = (double) $responseTemp->Items->Item->CustomerReviews->AverageRating;
echo $num * 2;
See Type Juggling and String Conversion to Numbers on the PHP website for more information.
If you are looking for a decimal value without doing typecasting, you have to multiply by a number with a decimal. Otherwise it will return a regular integer like the number you gave it.
Try multiplying by 2.0
echo $responseTemp->Items->Item->CustomerReviews->AverageRating*2.0;

Categories