2010-01-02 7 views

Répondre

23

En PHP, essayez number_format:

$n = 1234.5678; 

// Two decimal places, using '.' for the decimal separator 
// and ',' for the thousands separator. 
$formatted = number_format($n, 2, '.', ','); 
// 1,234.57 
4

Pour PHP, vous pouvez utiliser number_format(), pour MySQL utiliser la fonction FORMAT().

MySQL: http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_format

FORMAT(number, 2) 

Exemple:

mysql> SELECT FORMAT(12332.123456, 4); 
     -> '12,332.1235 

PHP: http://php.net/manual/en/function.number-format.php

$number = 1234.5678; 
$formatted_number = number_format($number, 2, '.', ''); 
// 1234.56 
+2

Vous pouvez également utiliser ROND() de la même façon si vous ne voulez pas le séparateur de milliers. –

-1

Vous pouvez multiplier votre nombre par 100, faites un arrondi du résultat puis diviser retour par 100.

Ou en php utiliser la fonction tour round function

$result=round(12.9241, 2); 
Questions connexes