|
 |
|
ทำให้ค่าเปอร์เซ็นต์ มันปัดเป็นสองหลักครับ คือผมทำเกี่ยวกับเปอร์เซ็นต์ แล้วค่าเปอร์เซ็นต์ที่ทำออกมา เป็นแบบนี้ครับ |
|
 |
|
|
 |
 |
|
Code (PHP)
<? echo number_format($objResult["quantity"]/$total[0]*100,2)?>
|
 |
 |
 |
 |
Date :
2009-10-12 19:46:30 |
By :
Sek-Artdrinker |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ sprintf ช่วย
$nums =$objResult["quantity"]/$total[0]*100;
$n = sprintf("%0.2f",$nums);
echo "$n %";
|
 |
 |
 |
 |
Date :
2009-10-12 19:53:21 |
By :
xbeginner01 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณพี่ๆ ทุกคนที่มาตอบครับ
|
 |
 |
 |
 |
Date :
2009-10-12 20:08:51 |
By :
nuttapont |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
fix ปัญหาปัดเศษผิดครับ ...หรือจะปัดผิดยิ่งกว่าเดิม 
<?php
function r($float, $fracCnt=2, $precision=6){
if ($fracCnt > 8 || $precision > 9){
//exceed 2147483647
return false;
}
if ($fracCnt >= $precision){
$precision = $fracCnt + 1;
}
$float = (string)$float;
list($n, $frac) = explode('.', $float);
$n = (int)$n;
$fracPre = substr($frac, 0, $precision);
$fracPre = (int)str_pad($fracPre, $precision, '0');
$fracLim = substr($frac, 0, $fracCnt);
$fracLim = (int)str_pad($fracLim, $precision, '0');
$fracPreLen = strlen($fracPre);
$prefixCnt = 0;
if ($fracPreLen < $precision){
if (!empty($fracPre)){
$prefixCnt = $precision - $fracPreLen;
if ($prefixCnt > $fracCnt){
$prefixCnt = $fracCnt;
}
}
}
$half = pow(10, ($precision - $fracCnt - 1)) * 5;
$fracLimHalf = $fracLim + $half;
if ($fracPre >= $fracLimHalf){
$fracRound = $fracLimHalf + $half;
if (strlen($fracRound) > $fracPreLen){
if (strlen($fracRound) > $precision){
$n += ($n > 0) ? 1 : -1;
$fracRound = 0;
$prefixCnt = 0;
} else {
$prefixCnt--;
if ($prefixCnt < 0 ){
$prefixCnt = 0;
}
}
}
}
else{
$fracRound = $fracLim;
}
$fracRoundLen = $fracCnt - $prefixCnt;
$fracRound = substr($fracRound, 0, $fracRoundLen);
$fracRound = str_pad($fracRound, $fracRoundLen, '0');
return ($n . '.' . str_repeat('0',$prefixCnt) . $fracRound);
}
echo '<br>';
echo round(2104.305,2);
echo '<br>';
echo round(2000.305,2);
echo '<br>';
echo number_format(2104.305,2);
echo '<br>';
echo number_format(2000.305,2);
echo '<br>';
echo sprintf('%0.2f',2104.305);
echo '<br>';
echo sprintf('%0.2f',2000.305);
echo '<hr>';
echo r(2104.305,2);
echo '<br>';
echo r(2000.305,2);
echo '<br>';
echo number_format(r(2104.305,2),2);
echo '<br>';
echo number_format(r(2000.305,2),2);
echo '<br>';
echo sprintf('%0.2f',r(2104.305,2));
echo '<br>';
echo sprintf('%0.2f',r(2000.305,2));
echo '<hr>';
ref: php-decimal-round
|
 |
 |
 |
 |
Date :
2009-10-12 20:26:51 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
 พี่หนุ่มเล่นเป็นฟังก์ชันเลยหรอ สุดยอดครับ  
พี่หนุ่มครับถามไรหน่อย
$n += ($n > 0) ? 1 : -1; หมายถึง ถ้า $n>0 แล้ว $n+=1 ไม่เช่นนั้นแล้ว$n+=(-1)
ผมเข้าใจความหมายถูกเปล่าครับ ลืมการเขียนแบบนี้ไปแล้ว 
|
 |
 |
 |
 |
Date :
2009-10-12 20:41:04 |
By :
xbeginner01 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่หนุ่มสุดยอดจริง อิอิ 
|
 |
 |
 |
 |
Date :
2009-10-12 21:39:33 |
By :
danya |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากๆ ทุกคนนะครับ
|
 |
 |
 |
 |
Date :
2009-10-12 22:01:53 |
By :
nuttapont |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
echo "round(($objResult["quantity"]/$total[0]*100,2)";
|
 |
 |
 |
 |
Date :
2009-10-12 22:13:17 |
By :
juder |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2009-10-12 23:09:50 |
By :
apicha31 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|