This question already has answers here:
mysql select int as currency or convert int to currency format?
(1 answer)
Print Currency Number Format in PHP
(8 answers)
Closed 4 years ago.
Having a lot of problems, I am using SUM on a field in my table and it works but only with one decimal place, I need it to be in currency format, I have tried the below code and others but nothing
$query = "SELECT purchase concat('$', sum(purchase), 2) from dtable";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "Total ". $row['SUM(purchase)'];
echo "<br />";
}
First off, please don't use the mysql* functions as they are deprecated. Move to the mysqli* functions. See the PHP Docs for more details.
How about this:
SELECT CONCAT('$', ROUND(SUM(`purchase`), 2)) AS `result`
FROM `dtable`
CONCAT just concatenates fields together so, in your case, you would end up with $###2. You need a function (like ROUND) to format the number as you want. ROUND take the number of desired decimal places as the first argument. More on ROUND (and other MySQL math functions) here: https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_round
You could also do this all in PHP using number_format.
Change
$query = "SELECT purchase concat('$', sum(purchase), 2) from dtable";
....
echo "Total ". $row['SUM(purchase)'];
to
$query = "SELECT SUM(`purchase`) AS `sumOfPurchase` FROM `dtable`";
....
echo 'Total $' . number_format($row['sumOfPurchase'], 2);
You can also use
SELECT CONCAT('$',FORMAT(sum(purchase),2)) as purchase from dtable
will give results like $1,234,567.89
Related
This question already has answers here:
How can I format a number into a currency value without php?
(3 answers)
Closed 4 years ago.
I am calculating result of my expression using eval() function but its showing all the numbers afer decimal and I want to show only two numbers after decimal
this is my eval function with other data that I have stored.
$expression = generate_expression($num_operands,
$operations, $num_digits);
$expression_string = implode(" ", $expression);
$result = eval("return ($expression_string);");
$expressions[] = compact("expression", "result");
I am storing the expression_
for this I tried using round function also but its not showing the result that I want
here is the round function for the $result
<?php echo round($result,2); ?>
Where I went wrong?
Use: number_format()
<?php echo number_format((float)$result, 2); ?>
Example
I am looking to generate a random number say 01:20 and then add 8 hours onto that and store that as a variable. However I want to do this within only the time and not use any random integers.
The date given for the query is found using a preset date at the moment set to 01-01-2017. StaffID is gotten from a loop through another table.
This is the PHP code snippet.
strtotime($random_hour = rand(00,23));
echo $random_hour . " Hour <br> ";
strtotime($random_min = rand(01,59));
echo $random_min . " Min <br> ";
$randomhourmin = $random_hour . ":" . $random_min;
echo $randomhourmin . "<br>";
This is the final SQL insert query.
$sql = "INSERT INTO schedule (staffID, cdate, starttime, endtime, dayoff) VALUES ('$rowID','$fDate','$randomhourmin','$randomhourmin','0')";
You can use below
$int= rand(1262055681,1262055681);
Also check mt_rand(), which is to have better randomness in the results:
$int= mt_rand(1262055681,1262055681);
To turn a timestamp into a string, you can use date(), ie:
$string = date("Y-m-d H:i:s",$int);
This question already has answers here:
Compare floats in php
(17 answers)
Closed 6 years ago.
I have been getting information from the database which adds the cost of each item in a table.
Every is working 100% apart from when the cost is equal to a curtain sum it does not become equal, it is always greater.
For example if 30.02 == 30.02 it should become true, but its saying it is greater than.
Basically I have a foreach loop which is getting the cost of each invoice from an array which goes like this:
$i_array = array('2','3','4','5');
$i_total = 0;
$a_total = 0;
foreach($i_array as $i){ $i_total += floatval($i); }
Then I have a little bit of code which checks all invoices and if there are any it then adds them like this:
$query = $pdo->prepare("SELECT cost FROM tablename WHERE id=id");
$query->execute(array(':id' => $_GET['id']));
while($fetch = $query->fetch()){
$a_total += floatval($fetch['cost']);
}
From there I do a simple if statement:
if($i_total > $a_total){ echo 'Too Much'; }
Now the problem is if i_total is lets say 30.02 and lets say the a_total is also 30.02 then it should be equal and it should not echo Too Much but its still echos Too Much.
If the i_total is lower or greater it is fine, just when its equal.
Anyone could shed some light on this it would be greatful :)
You could try with number_format(). Then you are comparing string values instead of float.
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I would like to know if it is possible to change the string format in an array. So in my example down below I would like to change the $doletzterwurm['date'] format from 0000-00-00(year-month-day) to day-month-year.
I know that I use the old mysql query, I will change it soonish ;)
<?php
$search1 = "SELECT * FROM `wurm_stats` ORDER BY `id` DESC LIMIT 1";
$letzterwurm = mysql_query($search1);
while ($doletzterwurm= mysql_fetch_array($letzterwurm))
{
echo "<br></br><div style='text-align:center;color:#FFFFFF;font-size: 24px;'>Letzter Wurm Eintrag am "
.$doletzterwurm['date'].
" von "
.$doletzterwurm['eingetragenvon'].
" </br>";
}
?>
It's possible, see the date function.
echo '[...]' . date('d-m-Y', $doletzterwurm['date']) . '[...]';
This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
I am making a simple order page the user will select an ammount on 1 page then on the next page it runs this
<?php
$bar = $_POST['bar'];
$sum= $bar * 1.59;
echo "Your price". $sum;
?>
the only problem we are having with this is that if they enter a certain ammount say 10 so for example 10 * 1.59 = 15.90 but it only shows 15.9 for any others it will show the ammount fully e.g for 5 it would show £7.95 - Is there something i could change in the code for it to always show the xx.xx figure instead of xx.x
thanks
I think that this is exactly what you're looking for: http://www.php.net/manual/en/function.money-format.php.
<?php
setlocale(LC_MONETARY, 'en_GB');
...
echo "Your price: " . money_format("%.2n", $sum);
?>
<?php
echo sprintf("Your price %.2f", $sum);
?>
You may use PHP function number_format.
<?php echo "Your price". number_format(number, 2, '.', '')); ?
http://php.net/manual/en/function.number-format.php