number_format

(PHP 4, PHP 5, PHP 7, PHP 8)

number_formatSayıyı binlik bölümlere ayırır

Açıklama

number_format(
    float $sayı,
    int $ondalık_hane = 0,
    ?string $ondalık_ayracı = ".",
    ?string $binlik_ayracı = ","
): string

sayı, yarıdan yukarı yuvarlanarak, ondalık kısmı ondalık_hane sayıda hane içerecek şekilde , ondalık ayraç olarak ondalık_ayracı, binlik ayraç olarak binlik_ayracı kullanılarak çıktılanır.

Bağımsız Değişkenler

sayı

Biçemlenecek sayı.

ondalık_hane

Ondalık hanelerin sayısı. 0 ise dönen dizgede ondalık_ayracı bulunmaz.

ondalık_ayracı

Ondalık ayracı.

binlik_ayracı

Binlik ayracı.

Dönen Değerler

sayı'nın biçemlenmiş sürümü.

Sürüm Bilgisi

Sürüm: Açıklama
8.0.0 Bu sürümden önce, number_format() bir, iki veya dört (üç değil) bağımsız değişken kabul ediyordu.
7.2.0 number_format() artık -0 döndüremiyor, evvelce sayı olarak -0.01 benzerlerinin belirtildiği durumlarda -0 dönebiliyordu.

Örnekler

Örnek 1 - number_format() örneği

Türkçede sayıları gösterirken genellikle iki ondalık hane, ondalık ayracı olarak virgül (','), binlik ayracı olarak nokta ('.') kullanırız.

<?php

$sayı
= 1234.56;

// İngilizcedeki gösterim (öntanımlı)
$english_format_number = number_format($sayı);
// 1,235

// Bizimki
$bizimki = number_format($sayı, 2, ',', '.');
// 1.234,56

$number = 1234.5678;

// İngilizcede binlik ayraçsız gösterim
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>

Ayrıca Bakınız

  • money_format() - Bir sayıyı para olarak biçemler
  • sprintf() - Biçemli bir dizge döndürür
  • printf() - Biçemli bir dizge çıktılar
  • sscanf() - Girdi dizgesini belli bir biçeme göre yorumlar

add a note

User Contributed Notes 7 notes

up
423
thomas at weblizards dot de
16 years ago
It's not explicitly documented; number_format also rounds:<?php$numbers = array(0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009);foreach ($numbers as $number)    print $number."->".number_format($number, 2, '.', ',')."<br>";?>0.001->0.000.002->0.000.003->0.000.004->0.000.005->0.010.006->0.010.007->0.010.008->0.010.009->0.01
up
8
info at ensostudio dot ru
3 years ago
Note: use NumberFormatter to convert in human-readable format instead  user function from comments:<?phpecho NumberFormatter::create('en', NumberFormatter::SPELLOUT)->format(12309); // twelve thousand three hundred nineecho NumberFormatter::create('ru', NumberFormatter::SPELLOUT)->format(12307.5); //  двенадцать тысяч триста семь целых пять десятых?>
up
38
james at bandit dot co.nz
16 years ago
Outputs a human readable number.<?php    #    Output easy-to-read numbers    #    by james at bandit.co.nz    function bd_nice_number($n) {        // first strip any formatting;        $n = (0+str_replace(",","",$n));                // is this a number?        if(!is_numeric($n)) return false;                // now filter it;        if($n>1000000000000) return round(($n/1000000000000),1).' trillion';        else if($n>1000000000) return round(($n/1000000000),1).' billion';        else if($n>1000000) return round(($n/1000000),1).' million';        else if($n>1000) return round(($n/1000),1).' thousand';                return number_format($n);    }?>Outputs:247,704,360 -> 247.7 million866,965,260,000 -> 867 billion
up
6
Jeroen de Bruijn [NL]
19 years ago
If you want to display a number ending with ,- (like 200,-) when there are no decimal characters and display the decimals when there are decimal characters i use:function DisplayDouble($value)  {  list($whole, $decimals) = split ('[.,]', $value, 2);  if (intval($decimals) > 0)    return number_format($value,2,".",",");  else    return number_format($value,0,".",",") .",-";  }
up
8
Theo Diem
22 years ago
formatting numbers may be more easy if u use number_format function.I also wrote this :function something($number){    $locale = localeconv();    return number_format($number,       $locale['frac_digits'],        $locale['decimal_point'],        $locale['thousands_sep']);}hope this helps =)[]'s
up
20
MarcM
19 years ago
For Zero fill - just use the sprintf() function$pr_id = 1;$pr_id = sprintf("%03d", $pr_id);echo $pr_id;//outputs 001-----------------$pr_id = 10;$pr_id = sprintf("%03d", $pr_id);echo $pr_id;//outputs 010-----------------You can change %03d to %04d, etc.
up
15
stm555 at hotmail dot com
20 years ago
I ran across an issue where I wanted to keep the entered precision of a real value, without arbitrarily rounding off what the user had submitted.I figured it out with a quick explode on the number before formatting. I could then format either side of the decimal.<?php      function number_format_unlimited_precision($number,$decimal = '.')      {           $broken_number = explode($decimal,$number);           return number_format($broken_number[0]).$decimal.$broken_number[1];      }?>
To Top