eliud liud
eliud liud

Reputation: 53

How to split long numbers by commas to readable number using php?

Am fetching a price from my database. It is being displayed using. It display the prices as follows: 5000000

How can i split it to display something like 5,000,000

Upvotes: 0

Views: 407

Answers (4)

Shailesh Dwivedi
Shailesh Dwivedi

Reputation: 685

<?php

$number = 1234.56;

// let's print the international format for the en_US locale
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
// USD 1,234.56

// Italian national format with 2 decimals`
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $number) . "\n";
// Eu 1.234,56

// Using a negative number
$number = -1234.5672;

// US national format, using () for negative numbers
// and 10 digits for left precision
setlocale(LC_MONETARY, 'en_US');
echo money_format('%(#10n', $number) . "\n";
// ($        1,234.57)

// Similar format as above, adding the use of 2 digits of right
// precision and '*' as a fill character
echo money_format('%=*(#10.2n', $number) . "\n";
// ($********1,234.57)

// Let's justify to the left, with 14 positions of width, 8 digits of
// left precision, 2 of right precision, withouth grouping character
// and using the international format for the de_DE locale.
setlocale(LC_MONETARY, 'de_DE');
echo money_format('%=*^-14#8.2i', 1234.56) . "\n";
// Eu 1234,56****

// Let's add some blurb before and after the conversion specification
setlocale(LC_MONETARY, 'en_GB');
$fmt = 'The final value is %i (after a 10%% discount)';
echo money_format($fmt, 1234.56) . "\n";
// The final value is  GBP 1,234.56 (after a 10% discount)

?>

Upvotes: 1

Karthi
Karthi

Reputation: 528

use money_format(string,number)

(Money format not fit for windows)

money_format

also use `number_format()

number_format — Format a number with grouped thousands

<?php echo number_format("5000000")."<br>"; ?>

number_format

Upvotes: 2

Webdev
Webdev

Reputation: 647

You need to use the php built in function number_format for this

echo number_format($price);

Upvotes: 1

Franz Gleichmann
Franz Gleichmann

Reputation: 3579

have you tried the manual?

number_format does exactly what you want:

<?php
$number = 5000000;
echo number_format($number); //output: 5,000,000

Upvotes: 4

Related Questions