PHP數(shù)字金額轉(zhuǎn)換成中文大寫顯示
整個(gè)功能其實(shí)不難,只是還算挺實(shí)用,所以記一下哈,其他編程語(yǔ)言轉(zhuǎn)換一下也是能可以的!
思路:把傳過(guò)來(lái)的金額轉(zhuǎn)換成整數(shù)和小數(shù)兩部分,再對(duì)其分別進(jìn)行轉(zhuǎn)換處理
代碼附上:
function moneyToString($num)
{
$digits = ['零', '壹', '貳', '叁', '肆', '伍', '陸', '柒', '捌', '玖'];
$radices =['', '拾', '佰', '仟', '萬(wàn)', '億'];
$bigRadices = ['', '萬(wàn)', '億'];
$decimals = ['角', '分'];
$cn_dollar = '元';
$cn_integer = '整';
$num_arr = explode('.', $num);
$int_str = $num_arr[0] ?? '';
$float_str = $num_arr[1] ?? '';
$outputCharacters = '';
if ($int_str) {
$int_len = strlen($int_str);
$zeroCount = 0;
for ($i = 0; $i < $int_len; $i++) {
$p = $int_len - $i - 1;
$d = substr($int_str, $i, 1);
$quotient = $p / 4;
$modulus = $p % 4;
if ($d == "0") {
$zeroCount++;
}
else {
if ($zeroCount > 0)
{
$outputCharacters += $digits[0];
}
$zeroCount = 0;
$outputCharacters .= $digits[$d] . $radices[$modulus];
}
if ($modulus == 0 && $zeroCount < 4) {
$outputCharacters .= $bigRadices[$quotient];
$zeroCount = 0;
}
}
$outputCharacters .= $cn_dollar;
}
if ($float_str) {
$float_len = strlen($float_str);
for ($i = 0; $i < $float_len; $i++) {
$d = substr($float_str, $i, 1);
if ($d != "0") {
$outputCharacters .= $digits[$d] . $decimals[$i];
}
}
}
if ($outputCharacters == "") {
$outputCharacters = $digits[0] . $cn_dollar;
}
if ($float_str) {
$outputCharacters .= $cn_integer;
}
return $outputCharacters;
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- PHP實(shí)現(xiàn)十進(jìn)制數(shù)字與二十六進(jìn)制字母串相互轉(zhuǎn)換操作示例
- php使用json_decode后數(shù)字對(duì)象轉(zhuǎn)換成了科學(xué)計(jì)數(shù)法的解決方法
- php 把數(shù)字轉(zhuǎn)換成漢字的代碼
- php實(shí)現(xiàn)阿拉伯?dāng)?shù)字和羅馬數(shù)字相互轉(zhuǎn)換的方法
- php短網(wǎng)址和數(shù)字之間相互轉(zhuǎn)換的方法
- PHP實(shí)現(xiàn)將科學(xué)計(jì)數(shù)法轉(zhuǎn)換為原始數(shù)字字符串的方法
- php中base_convert()進(jìn)制數(shù)字轉(zhuǎn)換函數(shù)實(shí)例
- PHP中IP地址與整型數(shù)字互相轉(zhuǎn)換詳解
- php導(dǎo)出csv格式數(shù)據(jù)并將數(shù)字轉(zhuǎn)換成文本的思路以及代碼分享
- PHP 金額數(shù)字轉(zhuǎn)換成英文
相關(guān)文章
IIS下PHP連接數(shù)據(jù)庫(kù)提示mysql undefined function mysql_connect()
在很多php教程初學(xué)者都會(huì)在初次php mysql時(shí)出來(lái)undefined function mysql_connect() 錯(cuò)誤提示,下面我們來(lái)分析原因中。2010-06-06
JavaScript實(shí)現(xiàn)滾動(dòng)欄效果的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)滾動(dòng)欄效果的方法,涉及javascript操作html元素實(shí)現(xiàn)滾動(dòng)的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
解析php做推送服務(wù)端實(shí)現(xiàn)ios消息推送
本篇文章是對(duì)php做推送服務(wù)端實(shí)現(xiàn)ios消息推送的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
php實(shí)現(xiàn)過(guò)濾表單提交中html標(biāo)簽的方法
這篇文章主要介紹了php實(shí)現(xiàn)過(guò)濾表單提交中html標(biāo)簽的方法,并以<br/>標(biāo)簽為例演示了過(guò)濾的方法,非常實(shí)用,需要的朋友可以參考下2014-10-10
如何用PHP實(shí)現(xiàn)分布算法之一致性哈希算法
進(jìn)行大型網(wǎng)站的web開(kāi)發(fā)時(shí),分布式這個(gè)詞經(jīng)常出現(xiàn)在我們面前。如: memcache、redis服務(wù)器等緩存服務(wù)器的負(fù)載均衡(分布式cache)、 MySQL的分布式集群,這些都會(huì)用到分布式的思想,都要理解分布式算法。接下來(lái)以緩存服務(wù)器的負(fù)載均衡來(lái)談一下一致性哈希算法。2021-05-05
做了CDN獲取用戶真實(shí)IP的函數(shù)代碼(PHP與Asp設(shè)置方式)
asp取真實(shí)IP的代碼,搭環(huán)境測(cè)試無(wú)代理、一級(jí)或多級(jí)代理的情況,可以正常獲取2013-04-04
解析mysql中UNIX_TIMESTAMP()函數(shù)與php中time()函數(shù)的區(qū)別
本篇文章是對(duì)mysql中UNIX_TIMESTAMP()函數(shù)與php中time()函數(shù)的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

