แปลงจำนวนเงิน เป็นตัวอักษรภาษาอังกฤษ
สวัสดีครับ
ผมขอรบกวนสอบถามเกี่ยวกับการแปลงจำนวนเงินแบบตัวเลข เป็นตัวอักษรภาษาอังกฤษครับ
พอดีผมไปเจอตัวอย่างในเว็บมา ดังนี้ครับ แล้วเหมือนจะมีปัญหาหลักแสน และหลักล้านมันไม่ค่อยโอเคครับ
ไม่ทราบว่าพี่ๆท่านไหน มีตัวอย่างที่ดีๆไหมครับ
ขอบคุณครับ
Code (PHP)
<?php
$number = $x;
$no = round($number);
$point = round($number - $no, 2) * 100;
$hundred = null;
$digits_1 = strlen($no);
$i = 0;
$str = array();
$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' => 'fourteen',
'15' => 'fifteen', '16' => 'sixteen', '17' => 'seventeen',
'18' => 'eighteen', '19' =>'nineteen', '20' => 'twenty',
'30' => 'thirty', '40' => 'forty', '50' => 'fifty',
'60' => 'sixty', '70' => 'seventy',
'80' => 'eighty', '90' => 'ninety');
$digits = array('', 'hundred', 'thousand', 'lakh', 'crore');
while ($i < $digits_1) {
$divider = ($i == 2) ? 10 : 100;
$number = floor($no % $divider);
$no = floor($no / $divider);
$i += ($divider == 10) ? 1 : 2;
if ($number) {
$plural = (($counter = count($str)) && $number > 9) ? 's' : null;
$hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
$str [] = ($number < 21) ? $words[$number] .
" " . $digits[$counter] . $plural . " " . $hundred
:
$words[floor($number / 10) * 10]
. " " . $words[$number % 10] . " "
. $digits[$counter] . $plural . " " . $hundred;
} else $str[] = null;
}
$str = array_reverse($str);
$result = implode('', $str);
$points = ($point) ?
"." . $words[$point / 10] . " " .
$words[$point = $point % 10] : '';
?>
Tag : PHP, MySQL
ประวัติการแก้ไข 2015-07-11 07:11:37
Date :
2015-07-10 22:39:01
By :
....................
View :
3187
Reply :
2
Code (PHP)
<?php
function numberTowords($num)
{
$ones = array(
0 =>"ZERO",
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 => "FOURTEEN",
15 => "FIFTEEN",
16 => "SIXTEEN",
17 => "SEVENTEEN",
18 => "EIGHTEEN",
19 => "NINETEEN",
"014" => "FOURTEEN"
);
$tens = array(
0 => "ZERO",
1 => "TEN",
2 => "TWENTY",
3 => "THIRTY",
4 => "FORTY",
5 => "FIFTY",
6 => "SIXTY",
7 => "SEVENTY",
8 => "EIGHTY",
9 => "NINETY"
);
$hundreds = array(
"HUNDRED",
"THOUSAND",
"MILLION",
"BILLION",
"TRILLION",
"QUARDRILLION"
); /*limit t quadrillion */
$num = number_format($num,2,".",",");
$num_arr = explode(".",$num);
$wholenum = $num_arr[0];
$decnum = $num_arr[1];
$whole_arr = array_reverse(explode(",",$wholenum));
krsort($whole_arr,1);
$rettxt = "";
foreach($whole_arr as $key => $i){
while(substr($i,0,1)=="0")
$i=substr($i,1,5);
if($i < 20){
/* echo "getting:".$i; */
$rettxt .= $ones[$i];
}elseif($i < 100){
if(substr($i,0,1)!="0") $rettxt .= $tens[substr($i,0,1)];
if(substr($i,1,1)!="0") $rettxt .= " ".$ones[substr($i,1,1)];
}else{
if(substr($i,0,1)!="0") $rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0];
if(substr($i,1,1)!="0")$rettxt .= " ".$tens[substr($i,1,1)];
if(substr($i,2,1)!="0")$rettxt .= " ".$ones[substr($i,2,1)];
}
if($key > 0){
$rettxt .= " ".$hundreds[$key]." ";
}
}
if($decnum > 0){
$rettxt .= " and ";
if($decnum < 20){
$rettxt .= $ones[$decnum];
}elseif($decnum < 100){
$rettxt .= $tens[substr($decnum,0,1)];
$rettxt .= " ".$ones[substr($decnum,1,1)];
}
}
return $rettxt;
}
extract($_POST);
if(isset($convert))
{
echo "<p align='center' style='color:blue'>".numberTowords("$num")."</p>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Conver Number to Words in PHP</title>
</head>
<body>
<form method="post">
<table border="0" align="center">
<tr>
<td>Enter Your Numbers</td>
<Td><input type="text" name="num" value="<?php if(isset($num)){echo $num;}?>"/></Td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Conver Number to Words" name="convert"/>
</td>
</tr>
</table>
</form>
</body>
</html>
https://www.studentstutorial.com/php/number-to-words
Date :
2020-06-04 21:39:30
By :
PhrayaDev
https://github.com/Rundiz/number
Code (PHP)
// For English require NumberEng.php, for Thai require NumberThai.php
require 'Rundiz/Number/NumberEng.php';
// For English use NumberEng(), for Thai use NumberThai()
$number_text = new Rundiz\Number\NumberEng();
echo $number_text->convertNumber('101');
// the result should be:
// one hundred and one (for English)
// หนึ่งร้อยเอ็ด (for Thai)
Date :
2020-06-04 21:58:49
By :
mr.v
Load balance : Server 04