Rounding Numbers

Started by oradba4u, June 25, 2015, 01:58:30 PM

Previous topic - Next topic

oradba4u

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

JTaylor

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

td

Code (winbatch) Select
; _Precision must be >= 0
#DefineFunction RoundIt(_fToRound, _Precision)
   return Floor((_fToRound * (10 ** _Precision)) + .5) / (10 ** _Precision)
#EndFunction
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

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


....IFICantBYTE

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))
Regards,
....IFICantBYTE

Nothing sucks more than that moment during an argument when you realize you're wrong. :)

oradba4u

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!





td

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."
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

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
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

I try to make that a habit but sometimes hard to convince people I'm always right  ;)

Jim