Home > Mobile >  How to format numbers in php
How to format numbers in php

Time:01-18

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: 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!

CodePudding user response:

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

  •  Tags:  
  • php
  • Related