Add two decimal values [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
so I want to add two values (money values) but can't quite work it out.
I keep getting a notice, I've tried number_format to format it but even using that I get the 'Notice: A non well formed numeric value encountered'
$price = 0.00;
while($row = $result->fetch_assoc()) {
$dbprice = $row["ProductSellPrice"];
$price = $price + $dbprice;
//echo number_format($price + $dbprice,2);
}
however I get this problem: Notice: A non well formed numeric value encountered
If I add two values '5' + '1.5' I get '6' as a result.
Edit:
The values I were pulling from the database were formatted with commas.

PHP numbers uses dot as separator and seems that you have coma. Please replace the coma with the dot as first. After that you can cast the prices into floats and finally sum it up :)

You can try with this (based on you example):
<?php
$price = 0.00;
while($row = $result->fetch_assoc()) {
$dbprice = $row["ProductSellPrice"];
// Replace comma. Check also for possible thousand separator.
$dbprice = str_replace(',', '.', $dbprice);
$price = $price + (float)$dbprice;
//echo number_format($price + $dbprice, 2);
}
?>

Related

how to iterate through string and get the second last number [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need second last number from the list and the list can have varied length. I tried seperating it using explode(",", $layouts);
$layouts= '1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,14,0,15,0,16,0,17,0,18,0,19,0,20,0,21,0,22,0,23,0,24,0,25,0,26,0,27,0,28,0,29,0,30,0';
Example : I need to get 30 from the string here.
please help
If this list can grow in size as you have indicated in the comment, then usually explode it into an array, then do a count of the size, subtract two from the total size and use that as an index to get the value:
//turn this to array
$layouts = '1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,14,0,15,0,16,0,17,0,18,0,19,0,20,0,21,0,22,0,23,0,24,0,25,0,26,0,27,0,28,0,29,0,30,0';
$layoutsArray = explode(",", $layouts);
$xtarget = count($layoutsArray) - 2;
$xvalue = "";
if($xtarget > -1){
$xvalue = $layoutsArray[$xtarget];
}else {
//it could be wrong
}
You can explode it with , and get 2nd last value from array try the following code
$layouts= '1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,0,14,0,15,0,16,0,17,0,18,0,19,0,20,0,21,0,22,0,23,0,24,0,25,0,26,0,27,0,28,0,29,0,30,0';
$array = explode(',',$layouts);
if(count($array) > 2){
$pair = array_slice($array, -2, 1, true);
$key = key($pair);
$value = current($pair);
echo $value;
}
Output
30

Php sum numbers with the same string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
i try sum numbers with the same string , for get one only number to the end , for example this :
<?php
$result.="1";
$result.="4";
$result.="8";
$result.="346";
$end +=result;
echo $end;
?>
I try do this but no get never the result ok , only show all numbers but not the result of the sum , by this my question , i don´t know if i writte something bad or similar
Thank´s for the help , regards
You are concatenating instead of adding.
<?php
$result+=1; // Need the += here instead of .=
$result+=4;
$result+=8;
$result+=346;
$end +=result; // Like you already did here.
echo $end;
The problem is that you are trying to add up STRINGs not INTs. You have to convert the values.
$result+=intval("1");
$result+=intval("4");
$result+=intval("8");
$result+=intval("346");
$end =$result;
echo $end;
This is not how you are supposed to do it, if you really want to store every number, just use an array :
<?php
$myNumber = [];
$myNumber[] = 1;
$myNumber[] = 4;
$sum = 0;
foreach($myNumber as $r)
$sum += $r;
echo $sum;
?>
$sum will now contain the sum of every number. You could also use the array_sum function to perform this operation.

Why does DateTime format return as a non-object on my second date format? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am getting an error that shows only if this DateTime format function is called a second time. It works fine on the first one, which is something I am assuming because the error only occurs on the line of the second call.
if( ! function_exists('month_dropdown')){
function month_dropdown($field_name = 'month', $selected = '01', $value_format = 'm', $atts = ''){
for($i=1;$i<=12;$i++){
$numObj = DateTime::createFromFormat('!'.$value_format, $i);
$val = $numObj->format($value_format);
$nameObj = DateTime::createFromFormat('!F', $i);
$text = $nameObj->format('F');
$months[$val] = $text;
}
return form_dropdown($field_name, $months, $selected, $atts);
}
}
This is the error I am getting:
Fatal error: Call to a member function format() on a non-object in ...application\frontend\helpers\MY_html_helper.php on line 73
Line 73 is where the variable $text is defined.
You're using the wrong formatting character. With month integers, use n
m and n
Numeric representation of a month, with or without leading zeros
Example: 01 through 12 or 1 through 12
For example
$nameObj = DateTime::createFromFormat('!n', $i);
$text = $nameObj->format('F');
Demo here ~ https://eval.in/188555

php round up decimals not working [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a bit of code but it is not echoing the correct answer, or in the correct format. I tried the round function but that didnt show anything
here is my code
$total_per_year = 308790;
$fat = $total_per_year / 3500;
echo $fat;
it is currently showing it as 0.088, but the correct answer is 88.2257142857
how can i do this so it round up to 88.2
The round function requires two parameters like so:
echo round($fat,1);
The second parameter being the amount of decimal places you require.
I have had issues previously with round not working as expected and I had to use number_format:
echo number_format((float)$fat,1,'.','');
With 1 being the decimal places required, the full stop being the decimal separator and '', being the thousand separator.
Use round()
echo round($fat, 1); //second argument is precision eg. decimals
See this for documentation: http://dk1.php.net/round
right, i worked this out now. I know i kept getting '0.088'. And this is wrong.
Its because i had it like this
$total_per_day = number_format($total_per_cup * $number_of_cups);
$total_per_year = number_format($total_per_day * 365);
$fat = round($total_per_year / 3500, 1);
echo $fat;
echo $total_per_year;
this is the correct code.
$total_per_cup = $type_calorie + $milk_calorie + $sugar_calorie;
$total_per_day = $total_per_cup * $number_of_cups;
$total_per_year = $total_per_day * 365;
$fat = $total_per_year / 3500;
echo json_encode(array('total'=> number_format($total_per_year) , 'perday' => number_format($total_per_day), 'fat'=> round($fat, 1)));

php string echoes as 0 when it shouldn't [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm writing code to show the prices of some products. If the price is 0 it doesn't show anything.
<? if ($book->price_euro != 0) {$europrice = number_format($book->price_eur, 2); echo "€$europrice";}?>
<? if ($book->price_gbp != 0) {$gbpprice = number_format($book->price_gbp, 2); echo "£$gbpprice";}?>
<? if ($book->price_usd != 0) {$usdprice = number_format($book->price_usd, 2); echo "$$usdprice";}?>
This echoes "€0.00 £33.00 $66.00 ". The € price is set to 99. I can see no reason whatsoever that this should echo as 0! Am I doing something wrong? Bad syntax?
$europrice = number_format($book->price_eur, 2
shouldn't it be $book->price_euro ?
Typo in Euro
number_format($book->price_eur, 2); // This is what you have.
number_format($book->price_euro, 2); // This is what you need.
I don't see anything wrong with this code without seeing more. But maybe your variable?
<? if ($book->price_euro != 0) {$europrice = number_format($book->price_eur, 2); echo "€$europrice";}?>
price_eur should be price_euro?
number_format($book->price_eur, 2);
should be
number_format($book->price_euro, 2);
The null value is causing your problem.
You might still be getting null values or something else try using empty instead
<? if (empty($book->price_euro)) {$europrice = number_format($book->price_euro, 2); echo "€$europrice";}?>
<? if (empty($book->price_gbp)) {$gbpprice = number_format($book->price_gbp, 2); echo "£$gbpprice";}?>
<? if (empty($book->price_usd)) {$usdprice = number_format($book->price_usd, 2); echo "$$usdprice";}?>

Categories