How to convert int into comma separated string in php? [closed] - php

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

Related

Format timezone offsets that starts with + or - [closed]

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 1 year ago.
Improve this question
How can I format a string to prepend UTC if it starts with + or - and add a : before the last two digits if it ends with 4 digits?
Examples and expected result:
PST > PST
+08 > UTC+08
-0845 > UTC-08:45
Thank you!
<?php
$string = "PST";
if (substr($string, 0, 1) === '+' || substr($string, 0, 1) === '-'){
if(strlen($string) == 3){
$newString = 'UTC'.$string;
}
else{
$newString = 'UTC'. substr($string, 0, 3). ':' .substr($string, -2, 2);
}
}
else{
$newString = $string;
}
echo $newString;
You don't need regex for that or at least regex seems overkill. You can simple create this logic using an if-else and modify your string accordingly

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

How to split 4113.52318N in two parts in php [closed]

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 split 4113.52318N in two parts like
41 and 13.52318N
And after that I want to remove N from the second value.
any help please?
There are several ways to do this, one is with preg_match_all, i.e.:
<?php
$string = "4113.52318N";
$result = preg_match_all('/^(\d{2})([\d.]+)/', $string, $matches);
$partOne = $matches[1][0]; //41
$partTwo = $matches[2][0]; //13.52318
Ideone Demo
Try this :
$part1 = substr('4113.52318N', 0, 2) // 41
$part2 = substr('4113.52318N', 3); // 13.52318N
$final = substr('4113.52318N', 0, -1); // 4113.52318
Use substr: http://php.net/manual/en/function.substr.php
$original = "4113.52318N";
$fortyone = substr($original, 0, 2); // 41
$other = substr($original, 3); // 13.52318N
$n_removed = substr($other, 0, -1); // 13.52318

php- how to randomize first and last two digits? [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 want to know how I can randomize first and last two digits in php? Suppose the current code is:
FJA793HIYX
then after randomizing it should be:
HJA793HIGD
How can I do that?
Try
$str = 'HJA793HIGD';
$filter = substr($str,1,7);
echo $newstr= randLetter().$filter.randLetter().randLetter();
function randLetter() {
$int = rand(0,26);
$alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$rand_letter = $alpha[$int];
return $rand_letter;
}
Try this!
$randomDigit = substr( "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ,mt_rand( 0 ,34 ) ,1 ) .substr( md5( time() ), 1)

How to compare two string values in php [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
<?php
$p= "php";
$P= "PHP";
echo ($p > $P);
?>
output: 1
it shows 1 meaning $p is greater than $P , but how are they compared i mean what are their values? and also how to display the values?
If you'd like to know how it works, a simple strcmp and ord functions can help you see them. Rough example:
http://php.net/manual/en/function.ord.php
Returns the ASCII value as an integer.
$p= "php";
$P= "PHP";
echo ord($p);
echo '<br/>';
echo ord($P);
echo '<br/>';
var_dump(strcmp($p, $P));
echo '<br/>';
var_dump($p > $P); // $p greater than $P
Output:
112
80
int(32)
bool(true)
It's based on each characters decimal value
http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters

Categories