How to format numbers in php


Hello i have this value 171024 and i want to make it 1,710.24 i tried the number_format but cant find the combination to give 1,710.24.

The code i did is:

echo number_format("171024");

and the result is 171,024

I followed this page:

but its limited not showing excaclty what i need.

Thank you in advance!

>Solution :

You want to format 1710.24, but you’re starting with 171024 so you need to do some arithmetic first

Convert your string to a float, then divide by 100: (float)"171024"/100

Then format the result, give it two decimal places and set the decimal and thousands separators:

echo number_format((float)"171024"/100,2,".",",");  // 1,710.24


Leave a Reply Cancel reply