Format de nombre, PHP

WRInaute passionné
Bonjour
Dans ma base j'ai des nombre (prix) du genre 2500000
Je voudrait afficher ce nombre sur le site comme ça:
2,500,000

Y a t-il une fonction PHP rapide pour faire ça ?

Merci
 
Nouveau WRInaute
Tu peux essayer ceci :
Code:
function ToMoney($string)
{
	$Negative = 0;
	
	if(preg_match('/^-/',$string))
	{
		$Negative = 1;
		$string = preg_replace('|-|','',$string);
	}
	
	$string = preg_replace('|,|','',$string);
	$Full = split('[.]',$string);
	$Count = count($Full);
	
	if($Count > 1)
	{
		$First = $Full[0];
		$Second = $Full[1];
		$NumCents = strlen($Second);
		if($NumCents == 2)
		{
			//do nothing already at correct length
		}
		else if($NumCents < 2)
		{
			$Second = $Second . '0';
		}
		else if($NumCents > 2)
		{
			$Temp = substr($Second,0,3);
			$Rounded = round($Temp,-1);
			$Second = substr($Rounded,0,2);
		}
	}
	else
	{
		$First = $Full[0];
		$Second = '00';
	}
	
	$length = strlen($First);
	
	if( $length <= 3 )
	{
		$string = $First . ',' . $Second;
		
		if($Negative == 1)
		{
			$string = '-' . $string;
		}
		
		return $string;
	}
	else
	{
		$loop_count = intval(( $length / 3 ));
		$section_length = -3;
		for($i = 0; $i < $loop_count; $i++)
		{
			$sections[$i] = substr($First, $section_length, 3);
			$section_length = $section_length - 3;
		}
		
		$stub = ($length % 3);
		if($stub != 0)
		{
			$sections[$i] = substr($First, 0, $stub);
		}
		
		$Done = implode(',', array_reverse($sections));
		$Done = $Done . ',' . $Second;
		
		if($Negative == 1)
		{
			$Done = '-' . $Done;
		}
		
		return $Done;
	}
}
 
Discussions similaires
Haut