您好我正在尝试显示百分比 像这样:10%,但我得到10.526315789474%
如何将其转换为10%
我的代码是:
$allUser = \Illuminate\Support\Facades\DB::table('users')
->leftjoin('member_type','users.memberTypeID','=','member_type.id')
->where('member_type.name','=','Client')->count();
$memberShip = \Illuminate\Support\Facades\DB::table('users')
->leftjoin('member_type','users.memberTypeID','=','member_type.id')
->where('member_type.name','=','Client')
->where('users.isActive','=','Y')->count();
$activeUser = ($memberShip/$allUser) * 100;
答案 0 :(得分:1)
取决于您想要做什么:
$activeUser = (int)(sprintf('%d', ($memberShip/$allUser) * 100)); // To round off
$activeUser = round( ($memberShip/$allUser) * 100); // To round up
$activeUser = floor( ($memberShip/$allUser) * 100); // To round down
答案 1 :(得分:-1)
Round可能就是你要找的东西
$activeUser = round(($memberShip/$allUser) * 100);
在laravel中你也可以像Blade一样使用{{round($activeUser)}}%
。
答案 2 :(得分:-1)
您可以将计算的百分比转换为整数,它将始终四舍五入到较低的整数。即10.9999将仅为10。