How to show decimal value after multiplication - php

I have value which stored in database like price. 123.00
When i tried to see the subtotal which is quantity * price.
So 123.00*1 = 123.00
But in view it shows 123 instead of 123.00 where price showing like 123.00 in same table.
How can i show decimal value after multiplication?

You need to format the number using number format PHP function. Consider
$price = 125;
$quantity = 1;
echo number_format($quantity*$price,2);
You can also have extra comma separated values that set the decimal separator, i.e. "." and the thousands separator also i.e. ",". See http://php.net/manual/en/function.number-format.php

Related

How to correctly do maths with higher numbers in PHP

I am trying to display the total amount which is product price times quantity.
This works perfectly for numbers under 1000, but above that a dot is added and my script breaks. How can I fix that?
I have these numbers:
150,00
1.200,00
They are looped with variable $price.
I then replace all commas for dots like this:
$subtotalreken = str_replace(',','.',$price);
$subtotalreken then contains:
150.00
1.200.00
I then multiply this with the quantity amount like this:
$totalfinal = $subtotalreken * $cart['quantity'];
The quantity is 2 for both products, and if I echo $totalfinal, this is my result:
300
2.4
Why is it 2.4? And not 2.400?
I need the european/dutch format, so dots for every three numbers and cents after the comma. How can I do that?
Assuming that the "number" uses comma as decimal and dots as thousands separator: remove all dots and replace the comma with dot so that 1.234,56 becomes 1234.56:
$value = (float) strtr("1.234,56", ["." => "", "," => "."]); // 1234.56
You can format the value again using number_format:
echo number_format($value, 2, ",", "."); // 1.234,56
Before calculations remove any formatting from your number (just make sure you always have 2 decimal places number):
$number = preg_replace('/[^\d]/', '', $input);
Then do your calculations (number is in cents) and show to user formatted number:
echo number_format($number * $qunatity / 100, '.', ',', 2);

PHP - Separate a string with comma

I am getting value from a API to get the amound spend.
Currently I got this:
Total invested: € 17101
Total invested(formatted): € Eu 17101,00
What I want:
I want to make the total invested string (fx 17101) to a currency number (171,01).
I have tried to do it with:
number_format($jsonData->amount_spent, 2, ',')
Sadly nothing was given the result I wanted
If the input is a string '17101' in Euro-Cents devide by 100 and then do the format:
$input = "17101";
echo number_format($input/100, 2, ',', '');
// output: 171,01
Also note that
This function accepts either one, two, or four parameters (not three):
from the Docs

Php ignores decimals while adding two numbers

When adding two number it ignores the decimals, in database the product_price and product_shipping_cost data type is decimal(10,2)
$product_price = 272.70; $product_price = 189.00;
$product_shipping_cost 14.00;
The total should be 475.70 but i get 475.7 as the output how do i fix this i have tried adding (float) but still same
foreach($mycart as $row_checker){
$CKItemSubtotal += (float)(($row_checker->product_discount>0) ? $row_checker->product_price * ((100-$row_checker->product_discount) / 100) * $row_checker->cart_qty : $row_checker->product_price * $row_checker->cart_qty) + $row_checker->product_shipping_cost;
}
Please can anyone help me
number_format($number, 2)
If two parameters are given, number will be formatted with decimals decimals with a dot (".") in front, and a comma (",") between every group of thousands.
You may also format number using printf. In your case: printf('%.2f', 475.7);, but that's up to you.

I'd like to format a record containing a number with a comma

I'd like the format of a record containing a number to be displayed with a comma separating every 1,000. The value in the record is the total price of a product.
In a very simplistic format:
$strSQL = "SELECT * FROM table WHERE model='A'";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['totalprice'] ;
The 'totalprice' record in the table for this example model contains the amount 32830
So the current output is 32830.
The desired output I'd like is 32,830
I can't hard code the number in the code as there are many products to display with different costs.
http://php.net/number_format
string number_format ( float $number , int $decimals , string $dec_point , string $thousands_sep )
So, what you want is:
echo number_format($row['totalprice']);
Because:
If only one parameter is given, number will be formatted without
decimals, but with a comma (",") between every group of thousands.

php round number include zero if .50

I want to round a number for e.g 269.00 and I round it like
round($price, 2)
which outputs 269 but when I have a value like 269.50 it ouputs 269.5 and I want to have the zero at the end this is for pricing products
Use number_format afterwards:
$price = number_format(round($price, 2), 2);
This also adds commas as thousands separators.

Categories