How 1000 10,000 1,00,000 number converting in php?

i mean splitting numbers like
100 1000 10000 100000
to
100 1,000 10,000 1,00,000
with ' , ' thousand, lack, crores...

Dani AI

Generated

is right that plain number_format() gives the Western 1,234,567 style. For Indian-style grouping (3,2,2,... for lakh/crore), use PHP’s intl NumberFormatter with the en_IN locale. It relies on ICU/CLDR data, so the commas land correctly without custom code. See the NumberFormatter docs and ICU’s grouping notes for en-IN. PHP NumberFormatter manual ICU grouping strategy example for en-IN

// Preferred: ICU/CLDR via intl
if (class_exists('NumberFormatter')) {
    $fmt = new NumberFormatter('en_IN', NumberFormatter::DECIMAL);
    // Optional: control decimals
    $fmt->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 0);
    $fmt->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 2);

    echo $fmt->format(100);      // 100
    echo "\n".$fmt->format(1000);    // 1,000
    echo "\n".$fmt->format(10000);   // 10,000
    echo "\n".$fmt->format(100000);  // 1,00,000
}

No intl? Here is a small, safe fallback that preserves negatives and decimals and applies the 3,2,2,... pattern. It works on strings, so you avoid float precision issues with large values. The two-size grouping is the same pattern CLDR describes as "#,##,##0" (eg. 12,34,56,789). CLDR number patterns

function format_indian_number($num) {
    $neg = str_starts_with((string)$num, '-') ? '-' : '';
    $n = ltrim((string)$num, '-');
    [$int, $frac] = array_pad(explode('.', $n, 2), 2, '');
    if (strlen($int) <= 3) return $neg.$int.($frac !== '' ? '.'.$frac : '');
    $last3 = substr($int, -3);
    $rest  = substr($int, 0, -3);
    $rest  = preg_replace('/\B(?=(\d{2})+(?!\d))/', ',', $rest);
    return $neg.$rest.','.$last3.($frac !== '' ? '.'.$frac : '');
}

// Examples
echo format_indian_number(100000);     // 1,00,000
echo "\n".format_indian_number(-1234567.89); // -12,34,567.89

Hope that gives you the concrete example you were looking for, and it builds on the pointer from .

Recommended Answers

All 6 Replies

Wow.. That's a big number.. I think You should have a look at PHP site that is posted up there.. .

ok finaly i got the code

function no_to_words($no){   
 $words = array('0'=> '' ,'1'=> 'one' ,'2'=> 'two' ,'3' => 'three','4' => 'four','5' => 'five','6' => 'six','7' => 'seven','8' => 'eight','9' => 'nine','10' => 'ten','11' => 'eleven','12' => 'twelve','13' => 'thirteen','14' => 'fouteen','15' => 'fifteen','16' => 'sixteen','17' => 'seventeen','18' => 'eighteen','19' => 'nineteen','20' => 'twenty','30' => 'thirty','40' => 'fourty','50' => 'fifty','60' => 'sixty','70' => 'seventy','80' => 'eighty','90' => 'ninty','100' => 'hundred &','1000' => 'thousand','100000' => 'lakh','10000000' => 'crore');
    if($no == 0)
        return ' ';
    else {
	$novalue='';
	$highno=$no;
	$remainno=0;
	$value=100;
	$value1=1000;       
            while($no>=100)    {
                if(($value <= $no) &&($no  < $value1))    {
                $novalue=$words["$value"];
                $highno = (int)($no/$value);
                $remainno = $no % $value;
                break;
                }
                $value= $value1;
                $value1 = $value * 100;
            }       
          if(array_key_exists("$highno",$words))
              return $words["$highno"]." ".$novalue." ".no_to_words($remainno);
          else {
             $unit=$highno%10;
             $ten =(int)($highno/10)*10;            
             return $words["$ten"]." ".$words["$unit"]." ".$novalue." ".no_to_words($remainno);
           }
    }
}

$org_num = "5255255";
echo "<br>org: ".$org_num; 
echo "<br>number_format: ".number_format($org_num) . '<br /> in words: ' .no_to_words($org_num);
Member Avatar for Member #120589

i got that. thanks for the repley i just post here for code example. r u tried that? have any idea

What does this mean? You mean that you're already aware of this, but just wanted to make somebody write code for you anyway?

You've been given a link that contains examples, so I'd say you're all solved.

commented: Better count to 1,000,000,000 ;) +14

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.