684425 Posted May 8, 2020 Share Posted May 8, 2020 In php number format function works like.... number_format("1000000",2); // 1,000,000.00 is there a way to format numbers like... 10,00,00,000 (from right. No decimal. First comma after 3 digits and then commas after every 2 digits) Quote Link to comment Share on other sites More sharing options...
Barand Posted May 8, 2020 Share Posted May 8, 2020 Here's one way <?php $n = 1; for ($i=0; $i<12; $i++) { printf("%15d | %-20s\n", $n, weird_format($n)); $n *= 10; } function weird_format($n) { if (($k = strlen($n)) < 4) return $n; $a = substr($n, 0, $k-3); // split off the last 3 chara $b = substr($n, -3); if (!($k%2)) $a = ' '.$a; // ensure first section is even no of chars $c = str_split($a, 2); // split into groups of 2 return trim(join(',', $c) . ',' . $b); // join them with commas } ?> Output: 1 | 1 10 | 10 100 | 100 1000 | 1,000 10000 | 10,000 100000 | 1,00,000 1000000 | 10,00,000 10000000 | 1,00,00,000 100000000 | 10,00,00,000 1000000000 | 1,00,00,00,000 10000000000 | 10,00,00,00,000 100000000000 | 1,00,00,00,00,000 1 Quote Link to comment Share on other sites More sharing options...
maxxd Posted May 8, 2020 Share Posted May 8, 2020 (edited) Sorry - misread the question. Edited May 8, 2020 by maxxd Quote Link to comment Share on other sites More sharing options...
benanamen Posted May 8, 2020 Share Posted May 8, 2020 12 hours ago, 684425 said: from right. No decimal. First comma after 3 digits and then commas after every 2 digits That is an interesting number format I have never seen. Could you please tell us what that is about and what it represents. Quote Link to comment Share on other sites More sharing options...
684425 Posted May 8, 2020 Author Share Posted May 8, 2020 3 hours ago, benanamen said: That is an interesting number format I have never seen. Could you please tell us what that is about and what it represents. In some countries 123456789 amount of currency of in general, it is counted as (if i am not wrong) one hundred twenty three million, four hundred fifty six thousand, seven hundred and eighty nine. Comma separated format is 123,456,789 But in some countries (like mine) the same amount is counted as twelve crore, thirty four lac, fifty six thousand seven hundred and eighty nine. Comma separated format is 12,34,56,789 As we people are used to the second format that is common here, the thousands separated format mostly confuses us in reading. Quote Link to comment Share on other sites More sharing options...
684425 Posted May 8, 2020 Author Share Posted May 8, 2020 (edited) 10 hours ago, Barand said: Here's one way <?php // $n = 1; // $n = 1000; $n = 100000; /* for ($i=0; $i<12; $i++) { printf("%15d | %-20s\n", $n, weird_format($n)); $n *= 10; } */ printf("%-20s\n", weird_format($n)); function weird_format($n) { if (($k = strlen($n)) < 4) return $n; $a = substr($n, 0, $k-3); // split off the last 3 chara $b = substr($n, -3); if (!($k%2)) $a = ' '.$a; // ensure first section is even no of chars $c = str_split($a, 2); // split into groups of 2 return trim(join(',', $c) . ',' . $b); // join them with commas } ?> Thank you for your help. With some modification, it works fine 🙂 Edited May 8, 2020 by 684425 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.