ตัวอย่าง php ฟังก์ชั่นแปลงค่าเงินตัวเลขเป็นตัวอักษรภาษาไทย
ตย. code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?php function Convert($amount_number) { $amount_number = number_format($amount_number, 2, ".",""); $pt = strpos($amount_number , "."); $number = $fraction = ""; if ($pt === false) $number = $amount_number; else { $number = substr($amount_number, 0, $pt); $fraction = substr($amount_number, $pt + 1); } $ret = ""; $baht = ReadNumber ($number); if ($baht != "") $ret .= $baht . "บาท"; $satang = ReadNumber($fraction); if ($satang != "") $ret .= $satang . "สตางค์"; else $ret .= "ถ้วน"; return $ret; } function ReadNumber($number) { $position_call = array("แสน", "หมื่น", "พัน", "ร้อย", "สิบ", ""); $number_call = array("", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า"); $number = $number + 0; $ret = ""; if ($number == 0) return $ret; if ($number > 1000000) { $ret .= ReadNumber(intval($number / 1000000)) . "ล้าน"; $number = intval(fmod($number, 1000000)); } $divider = 100000; $pos = 0; while($number > 0) { $d = intval($number / $divider); $ret .= (($divider == 10) && ($d == 2)) ? "ยี่" : ((($divider == 10) && ($d == 1)) ? "" : ((($divider == 1) && ($d == 1) && ($ret != "")) ? "เอ็ด" : $number_call[$d])); $ret .= ($d ? $position_call[$pos] : ""); $number = $number % $divider; $divider = $divider / 10; $pos++; } return $ret; } ## วิธีใช้งาน $num1 = '3500.01'; $num2 = '120000.50'; echo $num1 . " = " .Convert($num1),"<br>"; echo $num2 . " = " .Convert($num2),"<br>"; ?> |
ผลการทำงาน
3500.01 = สามพันห้าร้อยบาทหนึ่งสตางค์
120000.50 = หนึ่งแสนสองหมื่นบาทห้าสิบสตางค์
ขอบคุณ ตย. code จาก http://www.thaicreate.com/php/forum/068746.html