例如$num='7,57,800';
如何将$number
的值显示为7.57 Lakhs?
答案 0 :(得分:5)
这是功能:
function formatInIndianStyle($num){
$pos = strpos((string)$num, ".");
if ($pos === false) {
$decimalpart="00";
}
if (!($pos === false)) {
$decimalpart= substr($num, $pos+1, 2); $num = substr($num,0,$pos);
}
if(strlen($num)>3 & strlen($num) <= 12){
$last3digits = substr($num, -3 );
$numexceptlastdigits = substr($num, 0, -3 );
$formatted = makeComma($numexceptlastdigits);
$stringtoreturn = $formatted.",".$last3digits.".".$decimalpart ;
}elseif(strlen($num)<=3){
$stringtoreturn = $num.".".$decimalpart ;
}elseif(strlen($num)>12){
$stringtoreturn = number_format($num, 2);
}
if(substr($stringtoreturn,0,2)=="-,"){
$stringtoreturn = "-".substr($stringtoreturn,2 );
}
return $stringtoreturn;
}
function makeComma($input){
if(strlen($input)<=2)
{ return $input; }
$length=substr($input,0,strlen($input)-2);
$formatted_input = makeComma($length).",".substr($input,-2);
return $formatted_input;
}
答案 1 :(得分:0)
检查此插件 - http://archive.plugins.jquery.com/project/numberformatter
以下是您如何使用此插件的示例。
$("#salary").blur(function(){
$(this).parseNumber({format:"#,###.00", locale:"us"});
$(this).formatNumber({format:"#,###.00", locale:"us"});
});
只需更改区域设置..
有关更多示例和信息,请访问 - http://code.google.com/p/jquery-numberformatter/
我的示例来源:http://code.google.com/p/jquery-numberformatter/
希望这会有所帮助:)
答案 2 :(得分:0)
这是另一个仅供参考的解决方案:
<?php
# Output easy-to-read numbers
# by james at bandit.co.nz
function bd_nice_number($n) {
// first strip any formatting;
$n = (0+str_replace(",","",$n));
// is this a number?
if(!is_numeric($n)) return false;
// now filter it;
if($n>1000000000000) return round(($n/1000000000000),1).' trillion';
else if($n>1000000000) return round(($n/1000000000),1).' billion';
else if($n>1000000) return round(($n/1000000),1).' million';
else if($n>1000) return round(($n/1000),1).' thousand';
return number_format($n);
}
?>
答案 3 :(得分:0)
<?php //Credits are going to: @Niet-the-Dark-Absol
function indian_number_format($num){
$num=explode('.',$num);
$dec=(count($num)==2)?'.'.$num[1]:'.00';
$num = (string)$num[0];
if( strlen($num) < 4) return $num;
$tail = substr($num,-3);
$head = substr($num,0,-3);
$head = preg_replace("/\B(?=(?:\d{2})+(?!\d))/",",",$head);
return $head.",".$tail.$dec;
}
?>
问题:stackoverflow.com/questions/10042485