This article mainly introduces the content is about PHP two decimal places, has a certain reference value, now share to everyone, the need for friends can refer to
Recently in doing statistics this piece of content, contact about the number of data more,
Three functions are used to keep the decimal number after N bits, and then a simple introduction to three functions:
1, Number_format
Echo Number_format ("5000000"). " <br> "; Echo Number_format (" 5000000 ", 2)." <br> "; Echo Number_format (" 5000000 ", 2,", ",". ");
This function is mainly implemented by: Thousands of bits are grouped into formatted data.
Output results
5,000,000
5,000,000.00
5.000.000,00
2, sprintf
$num = 123; $data =sprintf ("%.2f", $num); Echo $data;
The result is:
123.00
3, round
$num 1 = 123.1267$data1 = Round ($num 1, 2), $num 2 = 123$data2 = Round ($num 2, 2); Echo $data 1. " <br> "Echo $data 2
The result is:
123.13
123
This function will not automatically help you if the decimal point is less than two bits.