유

기초 테스트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-12-02 조회 3,930
11-12-02 조회 3,647
11-12-02 조회 3,029
11-12-02 조회 3,627
11-12-02 조회 3,621
11-12-02 조회 3,454
11-12-02 조회 2,960
11-12-01 조회 2,877
11-12-01 조회 3,283
11-12-01 조회 2,810
11-12-01 조회 5,523
11-12-01 조회 2,853
11-12-01 조회 3,220
11-12-01 조회 2,853
11-12-01 조회 2,961
11-12-01 조회 4,145
11-12-01 조회 3,257
11-12-01 조회 3,830
11-12-01 조회 2,921
11-11-30 조회 3,542
11-11-30 조회 4,119
11-11-30 조회 3,004
11-11-30 조회 3,447
11-11-30 조회 3,242
11-11-30 조회 3,505
11-11-30 조회 4,904
11-11-30 조회 2,900
11-11-30 조회 7,059
11-11-30 조회 2,856
11-11-29 조회 3,135
11-11-29 조회 2,832
11-11-29 조회 5,800
11-11-29 조회 2,754
11-11-29 조회 2,998
11-11-29 조회 3,409
11-11-28 조회 3,490
11-11-28 조회 2,890
11-11-28 조회 3,460
11-11-28 조회 3,017
11-11-28 조회 2,803
11-11-28 조회 4,679
11-11-28 조회 3,270
11-11-28 조회 2,798
11-11-28 조회 2,763
11-11-27 조회 4,024
11-11-26 조회 3,026
11-11-26 조회 3,466
11-11-26 조회 3,777
11-11-26 조회 4,038
11-11-25 조회 4,349
11-11-25 조회 3,453
11-11-24 조회 2,944
11-11-24 조회 2,836
11-11-24 조회 3,019
11-11-24 조회 3,232
11-11-24 조회 4,766
11-11-24 조회 2,824
11-11-24 조회 3,026
11-11-24 조회 3,128
11-11-24 조회 3,641
11-11-24 조회 2,698
11-11-23 조회 2,613
11-11-23 조회 3,786
11-11-23 조회 3,707
11-11-23 조회 4,051
11-11-22 조회 3,149
11-11-22 조회 2,783
11-11-22 조회 4,042
11-11-22 조회 3,166
11-11-22 조회 3,634
11-11-22 조회 2,827
11-11-22 조회 3,133
11-11-22 조회 3,103
11-11-22 조회 2,994
11-11-22 조회 6,779
11-11-21 조회 2,858
11-11-21 조회 3,011
11-11-21 조회 3,659
11-11-21 조회 2,832
11-11-21 조회 2,513
11-11-21 조회 2,952
11-11-19 조회 4,425
11-11-19 조회 3,888
11-11-19 조회 3,495
11-11-18 조회 5,203
11-11-18 조회 4,036
11-11-18 조회 3,152
11-11-18 조회 3,028
11-11-18 조회 3,200
11-11-17 조회 3,084
11-11-17 조회 2,555
11-11-17 조회 2,922
11-11-17 조회 3,068
11-11-17 조회 4,645
11-11-16 조회 2,903
11-11-16 조회 2,615
11-11-16 조회 2,636
11-11-16 조회 5,010
11-11-16 조회 2,960
11-11-16 조회 3,641