php round number include zero if .50 - php

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.

Related

How to transform last two numbers to decimal (int to float) in php?

I need transform 100 to 1.00, 345 to 3.45 or any number with 3 digits or more to record in db like a decimal.
Don't need add .00, just transform last two numbers in decimal.
I try number_format($num, 2) but is wrong.
It seems, that you want to divide your numbers to 100, so using number_format() with the appropriate $decimal_separator and $thousands_separator is an option:
<?php
echo number_format($num / 100, 2, '.', '');
?>
Since you are converting from an integer to a float, you can achieve this simply by dividing the number by 100.
$input = 100;
$value = floatval($input) / 100;
$value = number_format($value, 2);
echo $value;
You may then use number_format (as you were using before) to force two decimal places after any evenly divided float numbers (such as 100).
Demo: PHP Sandbox Example

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

How to show decimal value after multiplication

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

PHP Number Formatting

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

How to delete trailing zeros after the 3rd decimal place in PHP?

thanks in advance.
I have a WP WooCommerce store and need to upload some prices that have 3 decimal places, e.g. £0.012 (products that are purchased in volumes of 000s).
The majority of my products are 'ordinary' with 2 decimal places.
There is a function in WooCommerce that allows for 3 decimal places - fine. Also a function to delete trailing zeros, but it deletes them if it's an integer e.g. £10.00 becomes £10.
My problem arises when the 95% of 'ordinary' price products start showing £10.000 or £5.230.
In a nutshell I'm looking for a way to delete trailing zeros but ONLY after the 3 decimal place;
Retain - £0.012
Delete any 3rd decimal 0 on prices like £10.00 or £5.23
Does anyone have a good solution?
Thanks
If you want to use regular expressions you can match them with
(?<=\d{2})(0+)$
preg_replace("/(?<=\d{2})(0+)$/", "", $price_string)
to match all zeroes which come after at least two digits. (It will match the zeroes in parenthesis):
12.002(0)
12.00(0000)
12.01(000000)
12.232(0)
12.123
an if else statement would probably work, unless you also have prices like 10.001:
$price = '0.001';
if ($price < 1) {
// don't round
} else {
$price = number_format($price, 2);
}
or just
$price = ( $price < 1 ) ? $price : number_format($price, 2) ;
Why not just something like this ↓ ?
$numberAsString = number_format($yourUglyNumber, 2, '.', ' ');
PHP function number_format
If you get the number as string with the money sign, you can first filter this out:
$moneyString = "£300.4578525";
// remove all non-numeric and cast to number
$moneyNum = preg_replace("/[^0-9.]/", "", $moneyString) + 0;
// format
$formatted = number_format($moneyNum, 2, '.', ' ');
// add the money symbol if you want
$formatted = '£' + $formatted.

Categories