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
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to convert int into comma separated string in php.
for example,
100000 into "1,00,000".
you can use number_format
$number=number_format(100000);
$echo $number; //output 100,000
or
$number=number_format(100000);
$newNumber=sprintf('"%s"',$number);
echo $newNumber ; //output "100,000"
you can use number_format() function
<?php
$number = '100000.457888';
echo number_format($number, 0); // returns 100,000
echo number_format($number, 1); // returns 100,000.5
echo number_format($number, 2); // returns 100,000.46
echo number_format($number, 3); // returns 100,000.458
echo number_format($number, 4); // returns 100,000.4579
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to create an array of integers from strings of text composed of the integers separated by whitespace and plus signs, for example
$string = "1 + 2 + 3 + 4";
is extracted into
$array = ('1' , '2' ,'3' , '4');
This needs to be done in php.
Thank you in advance for any help.
Thankyou for the question refinment.
<?php
$string = '1 + 2 + 3';
$array = explode(' + ', $string);
for ($a=0;$a<count($array);$a++){
$array[$a] = (int) $array[$a]; // RECAST STRING TO INTEGER
}
?>
To get the array you need to use PHPs explode function, which will split a string into an array by a delimiter, for example:
$integerString = '1 + 2 + 3 + 4';
$integerArray = explode(' + ', $integerString);
Use preg_match_all!
<?php
$string = '1 + 2 + 3';
preg_match_all("'[0-9]+'sim", $string, $out);
print_r($out);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to decompose a time in two part, I mean by that, I want to take the left side and the right side, like a cut off right ?
22:02:00
23:00:00
23:12:00
Imagine those number, it doesn't matter if there's seconds or not, so we can kick them
22:02
23:00
23:12
Now, I to take the separate hour and minute. How can we do that ?
The simplest way would be to just cut out the first 5 characters of the string:
$time = '22:02:00';
echo substr($time, 0, 5); // 22:02
You can also parse the time using e.g. the DateTime class:
$time = '22:02:00';
$parsed = DateTime::createFromFormat('H:i:s', $time);
echo $parsed->format('H:i');
As Mark Baker commented, the strptime() function can also be used:
$time = '22:02:00';
$parsed = strptime($time, '%H:%M');
echo str_pad($parsed['tm_hour'], 2, '0', STR_PAD_LEFT) . ':' . str_pad($parsed['tm_min'], 2, '0', STR_PAD_LEFT);
Regular expressions would also work:
$time = '22:02:00';
preg_match('/^(?P<hour>\d{2}):(?P<minute>\d{2})/', $time, $result);
if (count($result) > 0) {
echo "{$result['hour']}:{$result['minute']}";
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am just trying to figure out how I can get the actual digits of a figure that has been calculated within php and formatted to have 2 decimal places.
so say its calculated it to be 45.76 I am trying to figure out how I can get the 76 from it for an if statement. Basically I want it to look and just say that if it's 00 then remove them, if not, show them.
Thanks
Try :
function showDecimals($v){
$n = abs($v);
$whole = floor($n);
$fraction = $n - $whole;
return $fraction > 0
}
And...
if (showDecimals(10.15)){
//Show
}else{
//Remove?
}
You want to show a whole number if there is no decimal place, and 2 decimals of precision if not?
Method 1
function formatNumber($n) {
$n = round($n*100)/100;
return ''+$n;
}
This simply rounds it to 2 decimals of precision. Zero truncation is automatic.
Usage
echo formatNumber(0); //0
echo formatNumber(0.5); //0.5
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.9
echo formatNumber(1.896); //1.9
Method 2
Or if you 1.9 to display as 1.90, I suppose this would work:
function formatNumber($n) {
if ($n == 0)
return ''+$n;
$str = ''.round($n*100)/100;
$dotpos = strrpos('.', $str);
if (strlen(substr($str, $dotpos+1)) === 2)
$str .= '0';
return $str;
}
Usage:
echo formatNumber(0); //0
echo formatNumber(0.5); //0.50
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.90
echo formatNumber(1.896); //1.90
Edit: Accidentally posted broken version of method 2, should be fixed now.
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 7 years ago.
Improve this question
I have generate a 16 digit number and i want to extract one number from every 4 digits of that 16 digit number. For e.g: 1234567892345678. I want to extract 2 from 1234, 7 from 5678, 3 from 9034 & 7 from 5678. Then store it in another variable $a. the extraction will be in a random manner.
You can try this -
$d = '1234567892345678';
$s = str_split($d, 4); // split in 4 digits
$n = array_map(function($x) {
return substr($x, rand(0, 3), rand(1, 1)); // extract single digit random number
}, $s);
$n will hold the random numbers.
I might be late at answering this question but you can simply use strlen function along with for loop like as
$str = "1234567892345678";
for($i = 0; $i < strlen($str);$i += 4){
echo $str[$i+rand(0,3)];
}
Here you have a one-liner:
$s = $string[rand(0,3)].$string[rand(4,7)].$string[rand(8,11)].$string[rand(12,15)];
echo $s;
Another one-liner:
for($s='',$i=0;$i<10; $s.=$string[rand($i,$i+=3)]);
echo $s;