gpt4 book ai didi

php,四舍五入数字

转载 作者:行者123 更新时间:2023-12-04 00:31:14 25 4
gpt4 key购买 nike

我尝试了round函数,但是标准函数对我来说不太好(所有数字必须在一个函数中工作)。

我有数字:0.7555 和 0.9298

And how round i this case: 
0.7555 - 0.75
0.9298 - 0.93

谢谢

最佳答案

假设您的测试用例正是您想要的......

function customRound( $inVal , $inDec ){
return round( ( $inVal - pow( 10 , -1*($inDec+1) ) ) , $inDec );
}

使用此函数您将得到以下结果:

customRound( 0.7555 , 2 );
# Returns 0.75

customRound( 0.9298 , 2 );
# Returns 0.93

更新 - 如果使用 PHP v5.3.0 或更高版本

发现使用 round() 函数并采用正确的模式,会自动执行此操作。

round( 0.7555 , 2 , PHP_ROUND_HALF_DOWN );
# returns 0.75

round( 0.9298 , 2 , PHP_ROUND_HALF_DOWN );
# returns 0.93

关于php,四舍五入数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3968597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com