How can I add commas to numbers in PHP
from the php manual http://php.net/manual/en/function.number-format.php
I'm assuming you want the english format.
<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation with a decimal point and without thousands seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
my 2 cents
Add commas as thousands separator and floating point dot in php
Below will output 1,234,567.00
$example = "1234567";
$subtotal = number_format($example, 2, '.', ',');
echo $subtotal;
Syntax
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
But I advise you to use money_format which will formats a number as a currency string
Add commas as thousands delimiter in Arabic money number expression
Based on @JellyBean answer
$num = '۱۲۳۴۵۶۷۸۹۰';
$newNum = wordwrap($num , 6 , ',' , true );
This will echo out ۱۲۳,۴۵۶,۷۸۹,۰
First of, why 6? Because The chars uses 2 bytes.
Second, there's still a problem; What we want is this: ۱,۲۳۴,۵۶۷,۸۹۰
Solution
We can reverse it
$num = strrev('۱۲۳۴۵۶۷۸۹۰');
$newNum = strrev(wordwrap($num , 6 , ',' , true ));
echo $newNum;
۱,۲۳۴,۵۶۷,۸۹۰
POC: https://3v4l.org/OVeQQ
Formatting numbers with commas PHP
There is a clear doc for this function:
string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )
http://php.net/manual/de/function.number-format.php
How can I add commas to numbers in PHP (Session Value)
Have a look at the parameters shown here.
<?php echo number_format(($_SESSION["result"]), 3,",", "."); ?>
Adding commas to sql values using php explicitly
In response to a comment that this can be done in sql.
MySQL
Use FORMAT.
For an integer:
SELECT FORMAT(Field, 0);
The second flag is how many numbers after the decimal you want.
For a float with 2 decimal points
SELECT FORMAT(Field, 2);
PHP
This can be accomplished using number_format
From http://php.net/
<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
To enhance this, number_format tends to do some rounding. So you may want something better if you have floats (but maybe you don't, but if you do) ::
$final = 46000.12;
echo number_format( floor( $final*100 ) / 100, 2 );
Dagon makes a good point that you could also look into money_format
From http://php.net/
$number = 1234.56;
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
// USD 1,234.56
Need to add comma after every 3 digits using php
Edit:
as another answer suggested, there is a simple way to do this using number_format
:
echo number_format(1234); // 1,234
Original answer:
try this str_split
$price = 1234;
$price_text = (string)$price; // convert into a string
$price_text = strrev($price_text); // reverse string
$arr = str_split($price_text, "3"); // break string in 3 character sets
$price_new_text = implode(",", $arr); // implode array with comma
$price_new_text = strrev($price_new_text); // reverse string back
echo $price_new_text; // will output 1,234
Related Topics
Wordpress Woocommerce - Use Update_Post_Meta to Add Product Attributes
Laravel Many to Many Self Referencing Table Only Works One Way
Get Key of Multidimensional Array
Reload the Page on Hitting Back Button
Jquery Load() and PHP Variables
Refresh a Part of Webpage in PHP
iOS Push Notification Does Not Work When Using Crontab Scheduler
Phpexcel Download Using Ajax Call
Tcpdf Error: Some Data Has Already Been Output, Can't Send PDF File
Get the Metadata of an Order Item in Woocommerce 3
How to Specify Multiple Return Types on PHP 7
How to Reset MySQL Pointer Back to the First Row in PHP
Dirt-Simple PHP Templates... Can This Work Without 'Eval'
Post Arrays Not Showing Unchecked Checkboxes
Change the PHP Path to Mamps PHP