請問如何在小數點後第3個進行四捨五入?
例如: 1.2345 -> 1.235Prec(1.2345, 3), 只能做 1.234...T_T A = Prec(1.2345, 3)
B = Prec(1.2345, 4)
if (B - A) >= 0.0005 then A = A + 0.001
我不懂 AmiBroker, 純粹亂掰, 若有錯誤, 請一笑置之 So what's the problem?
function cRound( value, decplaces )
{
exponential = exp( log( 10 ) * decplaces );
rounded = int( value * exponential + 0.5 ) / exponential;
return rounded;
}
function cPrec( value, decplaces )
{
return Prec( value + 5*10^-(decplaces+1), decplaces );
}
printf( "round1: %g", cRound( 1.2345, 3 ) );
printf( "\nround2: %g", cPrec( 1.2345, 3 ) );
joshsmi 發表於 15-3-4 20:24 static/image/common/back.gif
So what's the problem?
joshsmi大大, 謝謝您啊, 正是想要的效果, {:4_161:}
不用這麼麻煩吧,把欲四捨五入的值加上0.0005 強迫進位,再捨掉四位後的小數即可。
A=1.2345
Prec(A+0.0005, 3)
就好了。
wldtw2008 發表於 15-3-5 11:41 static/image/common/back.gif
不用這麼麻煩吧,把欲四捨五入的值加上0.0005 強迫進位,再捨掉四位後的小數即可。
A=1.2345
Prec(A+0.0005 ...
嗯, 剛測試這方式, 亦是可產生四捨五入的效果.謝謝大大~{:5_260:}
wldtw2008 發表於 15-3-5 11:41 static/image/common/back.gif
不用這麼麻煩吧,把欲四捨五入的值加上0.0005 強迫進位,再捨掉四位後的小數即可。
A=1.2345
Prec(A+0.0005 ...
No, it is not enough as it is working for 3 decimal places only.
That's why
function cPrec( value, decplaces )
{
return Prec( value + 5*10^-(decplaces+1), decplaces );
}
頁:
[1]