ฟังก์ชัน number_format ใน PHP

ห่างหายไปนานมาอัพเดทฟังก์ชันของ PHP ที่ใช้บ่อยๆ กันหน่อยครับ อีกฟังก์ชันนึงที่ผมใช้อยู่บ่อยก็คือ number_format ครับ รูปแบบการใช้งานก็ไม่ได้ยุ่งยากครับ ส่วนใหญ่ผมจะใช้ในการแสดงค่่าเงินครับ

รูปแบบที่ 1

string number_format ( float $number [, int $decimals ] )

รูปแบบที่ 2

string number_format ( float $number , int $decimals , string $dec_point , string $thousands_sep )

มาดูตัวอย่างการใช้งานกันดีกว่าครับ

<?php
$number = 1234.56;
//englishnotation(default)
$english_format_number = number_format($number);
//1,235

//Frenchnotation

$nombre_format_francais = number_format($number, 2, ‘,’, ‘’);
//1234,56
$number = 1234.5678;

//englishnotationwithoutthousandsseperator


$english_format_number = number_format($number, 2, '.', '');
//1234.57
?>
ดูตัวอย่างเพิ่มเติมได้ที่นี่ครับ

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>