Jump to content

Recommended Posts

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
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.

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

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.