phpnumbers

How to truncate number 3 decimals


I would like to truncate numbers at 3 decimals with php :

3.236665   3.236
3.236111   3.236

Thanks


Solution

  • To truncate a number up to p decimals, multiply the number by the p-th power of 10, truncate the fraction by casting to an integer, and then divide the result by the p-th power of 10.

    The following truncates $n up to the 3rd decimal:

    intval($n * 1000) / 1000;