WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: oradba4u on June 25, 2015, 01:58:30 PM

Title: Rounding Numbers
Post by: oradba4u on June 25, 2015, 01:58:30 PM
All:
Does anybody have a user-defined function that can perform the following:

Pass in 2 values - RoundIt(ValueToBeRounded, NumberOfDecimals)

The function should round the ValueToBeRounded to NumberOfDecimal places.

Examples:
RoundIt(1.2,3)  = 1.200
RoundIt(.6,2) = .60
RoundIt(.059,4) = .0590
RoundIt(.1266,3) = .127

I've banged my head for several hours on this, and I'm out of ideas.

Any HELP would be greatly appreciated.

Thanks in Advance
Title: Re: Rounding Numbers
Post by: JTaylor on June 25, 2015, 04:06:32 PM
Will the Decimals() function do what you need?  Not sure what it does regarding rounding...it may just chop things off rather than round so may not be a viable option.

Jim
Title: Re: Rounding Numbers
Post by: td on June 25, 2015, 05:37:00 PM
Code (winbatch) Select
; _Precision must be >= 0
#DefineFunction RoundIt(_fToRound, _Precision)
   return Floor((_fToRound * (10 ** _Precision)) + .5) / (10 ** _Precision)
#EndFunction
Title: Re: Rounding Numbers
Post by: stanl on June 26, 2015, 02:03:07 PM
If you have Office, you can always use the worksheetfunction() which includes round(), rounduo(), rounddown() and mround.  It is a bit more overhead, and in the snippet below, I would keep the excel object open for multiple operations:

Code (WINBATCH) Select

gosub udfs

message("",roundit(.1266,3))
message("",mroundit(187,200)) ;round up to nearest 100
Exit
                 

:udfs
#DefineFunction roundit(n,d)
   oXL = CreateObject("Excel.Application")
   oXL.Visible = 1 ;change to 0 after testing that it works
   retval=oXL.WorksheetFunction.round(n,d)
oXL=0
Return(retval)
#EndFunction


#DefineFunction Mroundit(n,m)
   oXL = CreateObject("Excel.Application")
   oXL.Visible = 1 ;change to 0 after testing that it works
   retval=oXL.WorksheetFunction.mround(n,m)
oXL=0
Return(retval)
#EndFunction


Return

Title: Re: Rounding Numbers
Post by: ....IFICantBYTE on June 27, 2015, 09:24:58 PM
This will do exactly what you requested in the first post... including the formatting of the result.
Code (winbatch) Select

;This will round numbers up to 8 places while appending zeros to decimal place values shorter than requested precision and dropping the leading zero before the decimal point if less than 1.0
#DefineFunction RoundIt(number,places)
DecimalsWas = Decimals(places)
result = %number%; using the %% will pass the value of the temporary decimal change to the new variable, otherwise the original precision will return once the decimals are restored to the previous value.
;adjust the formatting of the returned value
Integer = Abs(Floor(result))
Decs = ItemExtract(2,result,".")
Decs = StrFix(Decs,0,places)
If Integer > 0
result = Integer:".":Decs
Else
result = ".":Decs
EndIf
Decimals(DecimalsWas)
Return result
#EndFunction

message("",RoundIt(1.2,3))
message("",RoundIt(.6,2))
message("",RoundIt(.059,4))
message("",RoundIt(.1266,3))
Title: Re: Rounding Numbers
Post by: oradba4u on June 28, 2015, 09:36:46 PM
Thanks to everyone who helped to solve this problem,

IfICantBYTE - Your solution was exactly what I was looking for! Elegant, yet fairly simple... MANY THANKS!




Title: Re: Rounding Numbers
Post by: td on June 29, 2015, 07:19:01 AM
It would appear that I need to take my own advice and RTFM.  From the 'Decimals' topic in the Consolidated WIL Help file:

"The floating point number will be rounded to the specified number of decimals."
Title: Re: Rounding Numbers
Post by: td on June 29, 2015, 09:34:56 AM
Jim had the correct idea all along because the Decimals setting not only rounds but also zero pads for you.  Can't get a whole lot simpler than this:
Code (winbatch) Select
#DefineFunction RoundIt3(_fToRound, _Precision)
   nPrecisionPrev = Decimals(_Precision)
   strResult      = _fToRound:""  ; Convert to string.
   Decimals(nPrecisionPrev)
   return strResult
#EndFunction
Title: Re: Rounding Numbers
Post by: JTaylor on June 29, 2015, 10:01:54 AM
I try to make that a habit but sometimes hard to convince people I'm always right  ;)

Jim