trouble with calculating percentage using PHP - 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

Related

How to convert 1.5xxE-5

I have the number 1.5020370483398E-5 but I want to show it as 0.00001502037. How can I convert 1.5020370483398E-5 to that number? Regular rounding functions do not work and instead just output 0.
You can use number_format()
https://www.php.net/manual/en/function.number-format.php
Here is an example on your case :
$test = 1.5020370483398E-5;
// Output : 0.00001502037
echo number_format($test, 11);

PHP and dynamic number formatting

I have looked for a way to do this and have not found it.
I have values read from MySQL: 100.00, 85.50, 97.00, 71.33
I want them to display as: 100, 85.5, 97, 71.33
I see number_format() that specifies FIXED decimal places, but I need a sort of 'significant digits format'
use (float)$number;
$a = '100.00';
$b = 73.50;
$c = 71.33;
echo (float)$a; // 100
echo (float)$b; // 73.5
echo (float)$c; // 71.33
you need to use floatval function to get your required output. just check below code.
var_dump(floatval('100.00'));
var_dump(floatval('85.50'));
var_dump(floatval('71.33'));

number formatting in php

I want to format numbers like following
13.20 to 13.2
13.34 to 13.34
13.00 to 13
I have tried to use a combination of str_replace() and number_format() but not able to produce required result.
Please help me, if anyone have any idea.
Thanks in advance
Try using the built-in round function:
echo round(1.23456, 2);
This will return 1.23. Of course, you have to decide how many numbers after the decimal point to keep.
number_format should work:
<?php
$number = 12.345;
echo number_format($number, 1); # Produces number with one decimal precision.
?>
EDIT:
<?php
$number = 12.30;
#Strips ZEROs and decimal point from the end
echo rtrim($number, '0.'); #Result: 12.3
?>
Or just simply, this is magic :
$number + 0
// 12.30 + 0 = 12.3
Or you can cast your number to float :
echo floatval($number);
It depends on the type of variable you're using. A string 13.20 will output 13.20 but a float 13.20 will output 13.2.
All you have to do is cast it:
echo (float) '13.20'; // Will output 13.2

Pricing issues with pounds and pence!

At the moment I store prices for products in the database as a pence number. So 4321 in the database means £43.21.
Then when reading it out, I divide by 100 to get it in pound and pence format.
However, I have a problem.
If the price is 4320, the returned value is 43.2 without the 0.
How can I get around this?
Thanks!
You can format strings with sprintf
See example 9:
<?php
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"
?>
<?
echo money_format("%i", 1234.5)
//Output: 1234.50
?>
You can use money_format.
money_format() should do the trick. Alternatively number_format() or the powerful printf().
echo number_format($float, 2, '.', '');
and for pretty printing of large values:
echo number_format($float, 2, '.', ',');

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

Categories