Reputation: 2810
Hello everyone i am trying to format the input number range with php number_format
dot should be in multiplier of 3 digit and last two (if available in input range) digits should be come with comma separater
out put should be as per dutch format and dot should be in multiplier of 3 digit and last two digits should be come with comma separater
like, here is link you can put any input value from above list in textbox you can get output like shown below
the code which i am using
echo numberFormat('12345');
function numberFormat($num)
{
return preg_replace("/(?<=\d)(?=(\d{3})+(?!\d))/",".",$num);
}
but it will not work with (4),(5),(6) from my input probability to match with outputs.
Upvotes: 3
Views: 4147
Reputation: 13088
This is pretty similar to @Emesto solution, but forced into a number:
$num = '123.456.789,50';
$n = str_replace('.', '', $num);
$n = str_replace(',', '.', $n);
# $n is no longer a string
$n = $n + 0;
# so it should work fine in number_format
echo number_format($n,2,',','.');
PS: I just tested this code to be sure that it works.
Upvotes: 1
Reputation: 4601
$temp=number_format((float)$number, 2, '.', '');
$inter =number_format($temp,2);
$temp1=str_replace(',','.',$inter);
$final=substr_replace($temp1,',',-3,1);
echo"$final";
Hope it helps
Upvotes: 0
Reputation: 1049
Well I think is like this:
$number = 1234.56
echo number_format($number, 2, ',', '.');
About the input yo could check and all the "." remove it and the "," replace with "."
$number = "123.456,78";
$temp = str_replace(".", "", $number);
$temp2 = str_replace(",", ".", $temp);
echo number_format($temp2, 2, ',', '.');
I hope it helps
Upvotes: 3