简体   繁体   中英

Convert MySQL number into currency via php?

I am pulling a number of out a MySQL database, at the moment it has no decimal formatting.

Is it possible to do this via php once it have been selected from the database via a SELECt function?

I would like it to convert from 2500 to £25.00?

Most likely very simple I just cant get my head around it!

Thanks

Check out money_format and number_format

Note that money_format only works on systems that support the C library function strfmon (Windows does not). Otherwise you'll need to use number_format or format it through SQL.

number_format为赢!

number_format($row['price'] / 100, 2, '.', ' ')

You could just use MySQL for this task before bothering PHP: FORMAT()
It has the huge advantage of the usage of the mysql locale and it doesn't require extra resources from PHP (which are minimal; but aren't we all minimalists?)

I use money_format for that. Here's the codez:

$value = 2500;
$value = $value / 100;
setlocale(LC_MONETARY, 'en_GB');
$value = money_format("%.2n", $value);
echo $value;

Output:

£25.00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM