2010-01-02 23 views

risposta

23

In PHP, provare 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

Per PHP è possibile utilizzare number_format(), per MySQL utilizzare la funzione FORMAT().

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

FORMAT(number, 2) 

Esempio:

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

È inoltre possibile utilizzare ROUND() nello stesso modo, se non si desidera che il delimitatore migliaia. –

-1

È possibile moltiplicare il numero per 100, fanno un arrotondamento del risultato e poi dividere indietro di 100.

O in php utilizzare la funzione rotonda round function

$result=round(12.9241, 2); 
Problemi correlati