기초 테스트2 > 토크

토크

개발과 관련된 어떤 얘기도 괜찮습니다.

기초 테스트2 정보

개발자 기초 테스트2

본문

round

주어진 값을 주어진 자리 아래에서 반올림 하여 반환
 
Returns the rounded value of val to specified precision
 
<?php
echo round(1.955832);  // 1.96
?>
 
문제> round 를 사용하지 않고 위와 동일한 문제에 동일한 답이 나오도록 처리하세요.
제한시간 - 2시간
추천
0
비추천
0

댓글 16개

디자이너로써 그냥>>
if 소수점 3자리째의 수가 5이상이면 2자리수에 +1시켜서 두자리수 까지만 출력해라.

else 이면 그냥 소수점두자리수까지만 표현해라.

라고 답글달아봅니다 ㅋ
echo xround(1.95583, 2);
function xround($rtn,$slength) {
$temp=1;
for ($i=1; $i<=$slength; $i++) $temp=$temp*10;
$rtn=$rtn*$temp+0.5;
return ((int) $rtn/$temp );
}

검사는 안해봣음 --; 대충 맞을것 같음

---> 검사해봄 잘됨
제한시간도 생겼네요 ㅠㅠ
최대한 네이티브로 했는데 is_numeric 은 예외처리로 끼워넣은거니까 봐주세요.

function round_custom($num,$pos=1){

if((int)$pos < 0) return 0;

if(is_numeric($num) == FALSE){
return 0;
}

$pos_helper = 1;
for($i = 0;$i<$pos;$i++){
$pos_helper*=10;
}

$constant_int_helper = 10;
$temp_var1 = (int)(($num*$pos_helper*$constant_int_helper + (($num*$pos_helper*$constant_int_helper)%10>4?10:-10))/$constant_int_helper)/$pos_helper;

return $temp_var1;
}
function banOlim($num, $r){

$roundingplace = pow(10,$r);
$num = $num*$roundingplace;
$num = ceil($num);

return $num / $roundingplace;

}

echo banOlim(1.95583, 2);
pow라는 함수가 있군요..... 허허~! 몰라서 for문 돌림 --;
ceil 은 반올림이 아닌것으로 이상합니다만.

banOlim(1.95583, 4) 해보시면 올림으로 나옵니다.
전체 3,599
토크 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1402호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT