Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I followed this page: https://www.w3schools.com/php/phptryit.asp?filename=tryphp_func_string_number_format

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

Demo: https://3v4l.org/QMumY

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading