ข้ามไปยังเนื้อหาหลัก
ProgrammerThailand

สังคมนักพัฒนาระบบ (Community)

ท่านไหนมีโค๊ดแปลงจากตัวเลขเป็นข้อความไหมครับ

<p>ตัวอย่างเช่น 123 เป็น หนึ่งร้อยยี่สิบสามบาทถ้วนครับ</p>
0 คะแนน

2 คำตอบ

0
Nattawuth Singhata Lv.1 Hello World ตอบ

ตัวอย่างเช่น 123 เป็น หนึ่งร้อยยี่สิบสามบาทถ้วนครับ

0
admin Lv.1 Hello World ตอบ · แก้ไข 18/8/14 19:02
	function tothai ($num = 0) {

		// split satang
	$floor = floor($num);
	
	$satang = $num - $floor;
	
	if ($satang > 0) { $satang = floor($satang * 100);  }
	
	$num = $floor;
	
	//
	
	$thai = "";
	
	$len = strlen($num);
	
	$tnum = split(" ","ศูนย์ หนึ่ง สอง สาม สี่ ห้า หก เจ็ด แปด เก้า"); 
	
	$tnth = split(" ","สิบ ร้อย พัน หมื่น แสน ล้าน สิบล้าน ร้อยล้าน พันล้าน แสนล้าน หมื่นล้าน ล้านล้าน");
	
	//
	
	foreach (str_split($num) as $k => $v) {
	
	$nth = $len - $k;
	
	if ($nth == 1 and $v == 1) { 
	
	$thai .= "เอ็ด"; 
	
	} elseif ($nth == 1 and $v == 0) {  
	
	$thai .= "";
	
	} else {
	
	$thai .= $tnum[$v];
	
	}
	
	$thai .= $tnth[$nth-2];
	
	}
	
	$thai .= "บาท";
	
	// SATANG
	
	if ($satang) { 
	
	foreach (str_split($satang) as $k => $v) {
	
	$nth = 2 - $k;
	
	if ($nth == 1 and $v == 1) { 
	
	$thai .= "เอ็ด"; 
	
	} elseif ($nth == 1 and $v == 0) {  
	
	$thai .= "";
	
	} else {
	
	$thai .= $tnum[$v];
	
	}
	
	$thai .= $tnth[$nth-2];
	
	}
	
	$thai .= "สตางค์";
	
	} else {
	
	$thai .= "ถ้วน";
	
	}
	
	// $thai .= "<br/>".$num . " ". $satang;
	
	return $thai;
	
	}

ที่มา http://lordbsd.blogspot.com/2013/07/php-bahttext.html

ตอบคำถามนี้

เข้าสู่ระบบเพื่อตอบคำถามและโหวต

เข้าสู่ระบบ สมัครสมาชิก