Page 1 of 1 1
Topic Options
#99607 - 2003-03-14 08:47 PM script to round numbers to specific decimal places
Radimus Moderator Offline
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
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#99608 - 2003-03-14 09:12 PM Re: script to round numbers to specific decimal places
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
The first part is easily done with a simple division
code:
$a=1755
$b=$a/100*100
[code]
or a combination of this and ROUND(). However, the second part is more complicated, but you could work with fractions in this case[code]
$a=1755
$b=$a/50*50

for a special case of 50, Others would be doable, too, but requires more logic.

[ 14. March 2003, 21:25: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.

Top
#99609 - 2003-03-14 10:05 PM Re: script to round numbers to specific decimal places
maciep Offline
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
#99610 - 2003-03-14 10:12 PM Re: script to round numbers to specific decimal places
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
The second part of the conditional doesn't really work.
code:
if $back > "5" or ($back = "5" and val($mid) mod 2)

there is a work around. I'm fixing it right now.

Done

[ 14. March 2003, 22:23: Message edited by: maciep ]
_________________________
Eric

Top
#99611 - 2003-03-14 11:39 PM Re: script to round numbers to specific decimal places
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Radimus,

Is the function I posted what you are looking for?
_________________________
Eric

Top
#99612 - 2003-03-15 01:03 AM Re: script to round numbers to specific decimal places
Glenn Barnas Administrator Offline
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! \:D

Top
#99613 - 2003-03-15 02:10 AM Re: script to round numbers to specific decimal places
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
Here's an alterative that rounds to 100, but has a "mid".

If the value is 0-29 it rounds down (120 -> 100)
If the value is 30-69 it rounds mid (167 -> 150)
If the value is 70-99 it rounds up (184 -> 200)

For values of 100 or more, you could actually look at two characters and set your rounding points to 25 and 75 instead of 30 and 70.

Here's the basic idea along with the test code..

code:
 
$X = fnRound(1796)
"1796 : $X" ?

$X = fnRound(398)
" 398 : $X" ?

$X = fnRound(702)
" 702 : $X" ?

$X = fnRound(746)
" 746 : $X" ?

$X = fnRound(452)
" 452 : $X" ?


Function fnRound($VAL)
$RND = 100

$fnRound = ($VAL / $RND) * $RND
$T = Val(Left(cStr($VAL - $fnRound), 1))
Select
Case $T > 7 ; greater than 7 - round up
$fnRound = $fnRound + $RND
Case $T > 3 ; greater than 3, less than 7, round mid
$fnRound = $fnRound + ($RND / 2)
EndSelect
EndFunction

_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 857 anonymous users online.
Newest Members
JimDunn, fgarcia, Viginette, ManuvdWielNL, Sir_Barrington
17890 Registered Users

Generated in 0.055 seconds in which 0.024 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org