#99607 - 2003-03-14 08:47 PM
script to round numbers to specific decimal places
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
I need a script/UDF that can accept a number and round it to a specific decimal place
1796 should round to 1800 398 should round to 400 702 should round to 700
bonus points if 746 would round to 750 and 452 would round to 450
|
|
Top
|
|
|
|
#99609 - 2003-03-14 10:05 PM
Re: script to round numbers to specific decimal places
|
maciep
Korg Regular
   
Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
|
code:
function round2($num, $howMany) dim $ret, $check, $front, $back, $mid $ret = ""+$num if $howMany >= len($ret) or $howMany < 1 $round2 = 0 return endif
$front = substr($ret, 1, len($ret)-$howMany-1) $mid = substr($ret, len($ret)-$howMany, 1) $back = substr($ret, len($ret) - $howMany+1) $check = "5" for $i = 1 to len($back)-1 $check = $check + "0" next if substr($back,1, 1) > "5" or ($back = $check and val($mid) mod 2) $mid = val($mid)+1 endif $ret = $front + "" + $mid for $i = 1 to len($back) $ret = $ret + "0" next $round2 = val($ret) endfunction
For example using your numbers,
code:
$r = round2(1796, 2) $r = round2(398, 2) $r = round2(702, 1) ; or $r = round2(702, 2) $r = round2(746, 1) $r = round2(752, 1)
[ 14. March 2003, 22:22: Message edited by: maciep ]
_________________________
Eric
|
|
Top
|
|
|
|
#99612 - 2003-03-15 01:03 AM
Re: script to round numbers to specific decimal places
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
|
How about the 4 lines in the fRound() function? You need to tell it to round to 10, 100, etc.
Glenn
code:
$X = fRound(1796,100) "1796 / 100 : $X" ? $X = fRound(398,100) " 398 / 100 : $X" ? $X = fRound(702,10) " 702 / 10 : $X" ? $X = fRound(702,100) " 702 / 100 : $X" ? $X = fRound(746,100) " 746 / 100 : $X" ? $X = fRound(746,10) " 746 / 10 : $X" ? $X = fRound(452,100) " 452 / 100 : $X" ? $X = fRound(452,10) " 452 / 10 : $X" ?
; return a number rounded to 10, 100, 1000, ... ; should error-check that RND is even multiple of 10 Function fRound($VAL, $RND) $fRound = ($VAL / $RND) * $RND If Val(Left(cStr($VAL - $fRound), 1)) >= 5 $fRound = $fRound + $RND EndIf EndFunction
Or, if you want to specify # digits (up to 9) or placevalue, try:
code:
$X = fRound(1796,1000) "1796 / 1000 : $X" ? $X = fRound(398,100) " 398 / 100 : $X" ? $X = fRound(702,10) " 702 / 10 : $X" ? $X = fRound(702,100) " 702 / 100 : $X" ? $X = fRound(746,100) " 746 / 100 : $X" ? $X = fRound(746,10) " 746 / 10 : $X" ? $X = fRound(452,100) " 452 / 100 : $X" ? $X = fRound(452,10) " 452 / 10 : $X" ? ? $X = fRound(1796,4) "1796 / 4 : $X" ? $X = fRound(398,3) " 398 / 3 : $X" ? $X = fRound(702,2) " 702 / 2 : $X" ? $X = fRound(702,3) " 702 / 3 : $X" ? $X = fRound(746,3) " 746 / 3 : $X" ? $X = fRound(746,2) " 746 / 2 : $X" ? $X = fRound(452,3) " 452 / 3 : $X" ? $X = fRound(452,2) " 452 / 2 : $X" ? $X = fRound(452,1) " 452 / 1 : $X" ? ; return a number rounded to 10, 100, 1000, ... ; should error-check that RND is even multiple of 10 Function fRound($VAL, $RND) If $RND < 10 $RV = $RND - 1 If $RV < 1 $fRound = -1 Exit 87 Else $RND = "1" For $X = 1 to $RV $RND = $RND + "0" Next $RND = Val($RND) EndIf EndIf $fRound = ($VAL / $RND) * $RND If Val(Left(cStr($VAL - $fRound), 1)) >= 5 $fRound = $fRound + $RND EndIf EndFunction
[ 15. March 2003, 01:17: Message edited by: Glenn Barnas ]
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 857 anonymous users online.
|
|
|