유

기초 테스트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-10-18 조회 2,672
11-10-18 조회 2,598
11-10-18 조회 2,512
11-10-17 조회 3,881
11-10-17 조회 5,157
11-10-17 조회 2,511
11-10-17 조회 2,784
11-10-17 조회 3,877
11-10-16 조회 2,539
11-10-16 조회 3,193
11-10-16 조회 3,641
11-10-15 조회 1.2만
11-10-15 조회 3,313
11-10-15 조회 3,049
11-10-15 조회 3,727
11-10-15 조회 3,721
11-10-15 조회 2,951
11-10-14 조회 2,973
11-10-13 조회 2,748
11-10-13 조회 4,518
11-10-12 조회 2,545
11-10-12 조회 2,715
11-10-12 조회 3,815
11-10-12 조회 4,112
11-10-12 조회 2,464
11-10-11 조회 2,979
11-10-11 조회 2,625
11-10-11 조회 2,801
11-10-11 조회 3,114
11-10-10 조회 2,785
11-10-10 조회 2,762
11-10-10 조회 2,467
11-10-10 조회 2,467
11-10-10 조회 2,741
11-10-10 조회 2,856
11-10-09 조회 2,670
11-10-08 조회 2,660
11-10-08 조회 4,586
11-10-08 조회 2,943
11-10-07 조회 3,168
11-10-07 조회 2,993
11-10-07 조회 3,491
11-10-06 조회 4,081
11-10-06 조회 2,640
11-10-06 조회 2,727
11-10-06 조회 3,934
11-10-06 조회 2,777
11-10-06 조회 4,323
11-10-06 조회 2,557
11-10-05 조회 2,855
11-10-05 조회 2,973
11-10-05 조회 4,067
11-10-04 조회 2,955
11-10-04 조회 5,404
11-10-04 조회 2,860
11-10-04 조회 2,408
11-10-03 조회 2,796
11-10-02 조회 2,680
11-10-02 조회 3,411
11-10-01 조회 2,439
11-10-01 조회 2,746
11-09-30 조회 2,636
11-09-30 조회 2,739
11-09-30 조회 2,918
11-09-30 조회 2,930
11-09-30 조회 3,696
11-09-30 조회 2,697
11-09-30 조회 3,123
11-09-29 조회 4,945
11-09-29 조회 2,772
11-09-29 조회 2,687
11-09-29 조회 3,081
11-09-28 조회 3,182
11-09-28 조회 3,952
11-09-28 조회 3,154
11-09-28 조회 2,531
11-09-28 조회 2,486
11-09-28 조회 2,352
11-09-28 조회 2,643
11-09-28 조회 4,147
11-09-26 조회 2,571
11-09-26 조회 2,633
11-09-26 조회 2,748
11-09-25 조회 3,095
11-09-24 조회 2,586
11-09-23 조회 2,854
11-09-23 조회 3,441
11-09-23 조회 2,570
11-09-23 조회 2,968
11-09-23 조회 2,312
11-09-22 조회 2,489
11-09-22 조회 2,900
11-09-21 조회 3,267
11-09-21 조회 3,299
11-09-20 조회 2,861
11-09-20 조회 2,693
11-09-20 조회 3,218
11-09-19 조회 2,736
11-09-19 조회 6,020
11-09-19 조회 3,824