유

기초 테스트2

round

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

댓글 16개

ㅠㅠ 저만 풀면 좀 그런거 같은데
어때요.... 다 같은 답이 나오는 것도 아니고 ..
그냥 풀면 됩니다.
디자이너로써 그냥>>
if 소수점 3자리째의 수가 5이상이면 2자리수에 +1시켜서 두자리수 까지만 출력해라.

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

라고 답글달아봅니다 ㅋ
echo "1.96";

ㅋㅅㅋ......
이건 쉽네요... ((int)1.95583 * (10 ^ 2) +9))/ 100)
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;
}
echo ((int)((1.95583 * 100) + .9)) / 100;
echo (int)((1.95583 + 0.005) * 100) / 100;
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) 해보시면 올림으로 나옵니다.
<?
function xround($float, $precision=0) {
$num = pow(10, $precision);
return (int)(($float + (0.5 / $num)) * $num) / $num;
}

echo xround(1.95583, 2);
echo number_format(1.95583, 2);
printf("%0.2f", 1.95583);
sprintf("%01.2f", 1.95583);
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,187건
+
제목 글쓴이 날짜 조회
11-07-16 조회 2,952
11-07-16 조회 3,115
11-07-15 조회 2,623
11-07-13 조회 3,544
11-07-13 조회 2,780
11-07-12 조회 3,417
11-07-12 조회 2,995
11-07-12 조회 2,609
11-07-12 조회 4,790
11-07-12 조회 3,195
11-07-12 조회 2,588
11-07-12 조회 2,436
11-07-12 조회 3,359
11-07-11 조회 2,568
11-07-11 조회 2,410
11-07-11 조회 2,329
11-07-11 조회 3,706
11-07-11 조회 2,289
11-07-11 조회 2,536
11-07-11 조회 3,299
11-07-11 조회 2,462
11-07-09 조회 2,532
11-07-08 조회 3,063
11-07-08 조회 2,693
11-07-08 조회 2,800
11-07-08 조회 2,601
11-07-07 조회 3,961
11-07-07 조회 2,627
11-07-07 조회 2,245
11-07-06 조회 2,466
11-07-06 조회 2,295
11-07-05 조회 2,304
11-07-05 조회 2,196
11-07-05 조회 2,449
11-07-05 조회 2,759
11-07-04 조회 2,996
11-07-03 조회 2,670
11-07-02 조회 2,702
11-07-02 조회 2,500
11-07-01 조회 2,627
11-07-01 조회 2,912
11-07-01 조회 2,728
11-07-01 조회 3,041
11-06-29 조회 2,776
11-06-28 조회 2,589
11-06-28 조회 2,634
11-06-28 조회 2,612
11-06-28 조회 3,175
11-06-28 조회 3,221
11-06-28 조회 2,902
11-06-27 조회 3,070
11-06-27 조회 3,213
11-06-27 조회 2,166
11-06-27 조회 2,703
11-06-27 조회 2,131
11-06-26 조회 2,469
11-06-26 조회 2,898
11-06-26 조회 2,409
11-06-26 조회 2,445
11-06-25 조회 2,650
11-06-24 조회 2,793
11-06-24 조회 2,695
11-06-24 조회 2,434
11-06-24 조회 2,749
11-06-23 조회 2,887
11-06-23 조회 2,833
11-06-23 조회 2,419
11-06-23 조회 3,635
11-06-23 조회 1,953
11-06-22 조회 2,637
11-06-22 조회 2,445
11-06-22 조회 2,318
11-06-22 조회 2,893
11-06-22 조회 3,274
11-06-21 조회 2,715
11-06-21 조회 2,335
11-06-21 조회 2,363
11-06-20 조회 2,608
11-06-17 조회 2,937
11-06-16 조회 3,063
11-06-16 조회 2,103
11-06-16 조회 2,594
11-06-15 조회 2,563
11-06-15 조회 2,552
11-06-15 조회 3,307
11-06-15 조회 3,139
11-06-15 조회 3,007
11-06-14 조회 2,723
11-06-14 조회 2,708
11-06-14 조회 3,329
11-06-13 조회 2,525
11-06-13 조회 2,152
11-06-13 조회 2,456
11-06-13 조회 2,351
11-06-13 조회 2,871
11-06-13 조회 3,118
11-06-13 조회 2,819
11-06-12 조회 2,489
11-06-10 조회 2,577
11-06-10 조회 2,604