Whitespace between numbers [closed] - php

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 7 years ago.
Improve this question
I would like to set a whitespace between numbers bigger than 999.
So for example:
Not "1000" but "1 000",
Not "10500" but "10 500";
How to do it while formatting a string?

Try with number_format, something like this:
<?php
$number = 10500;
echo number_format($number, 0, '.', ' ');
// Output 10 500
?>
This should do the trick.

Try using number_format() function.
Example:
$a = 1000;
echo number_format($a, 0, '.', ' ');

You maybe looking for the php function number_format()
$number= 1000;
if($number > 999){
$number = number_format($number, 0, '', ' ');
echo $number;
}
DEMO

You could use the substr() function inside of php

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

Best approach to explode high values [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
What's best approach to explode and separate high numeric values so it will be displayed in more legible way?
For example
100000000 should be converted to 100 000 000, or 10000.00 to 10 000.00
Use the number_format function.
$number = 1234.56;
number_format($number, 2, ',', ' '); // 1 234,56
As it's tagged as PHP you are looking for function called
number_format()
More details how to use it in documentation http://php.net/manual/en/function.number-format.php
Try this:-
$value = 100000000;
echo number_format($value , 0, ' ', ' ');
Output:- 100 000 000
number_format() function will accept one, two, and four arguments. Not three. and it works as follows:
// formatting with ","
$number = 1234.56;
var_dump(number_format($number)); // 1,235
//formatting with decimals
$number = 1234.56;
var_dump(number_format($number, 2)); // 1,235.56
//formatting with thousands seperator
$number = 1234.56;
var_dump(number_format($number, 2, '.', '')); // 1235.56

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.

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