Jump to content

Recommended Posts

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)

Share this post


Link to post
Share on other sites

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 

 

  • Like 1

Share this post


Link to post
Share on other sites
Posted (edited)

Sorry - misread the question.

Edited by maxxd

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Posted (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 by 684425

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.