Question

I have a data file where decimal points aren't specified for a decimal number. The number is just described in the layout for the data file as first 2 digits as real and next 2 digits as decimal and it varies for different fields, the real and decimal part

So an actual number 12345.6789 is specified as 123456789. When I want this to be rounded off to 2 decimal points to match the value in application, I use the below logic

Public Function Rounding(NumberValue, DecimalPoints, RoundOff)

        Rounder= Roundoff+1
    Difference = DecimalPoints - Rounder
    NumberValue = Mid(NumberValue, 1, Len(NumberValue)-Difference)

    RealNumber=Mid(NumberValue,1,Len(NumberValue)-Rounder)
    DecimalNumber=Right(NumberValue,Rounder)

    NumberValue = RealNumber&"."&DecimalNumber
    NumberValue = Cdbl(NumberValue)
    NumberValue = Round(NumberValue, Roundoff)
    Rounding = FormatNumber(NumberValue,Difference+1,,,0)

End Function

However the problem with this logic is that I am not able to round off decimals when the number has 0 as the decimal value

For an Example, lets take 12345.0000 which I want to round off to 2 decimal points

My function returns it as 12345 whereas I want this to be returned as 12345.00

Any ideas on how this logic could be tweaked to get the desired output or is that not possible at all?

Was it helpful?

Solution

To get the decimal places, use the Formatnumber function. See http://msdn.microsoft.com/en-us/library/ws343esk(v=vs.84).aspx - the default is normally 2 decimal places, but it is region settings specific when using the defaults.

Your script also has a small issue if the decimalpoints variable matches the roundoff variable - it will not populate Rounding with a result. I am also not sure why you are comparing DecimalPoints to Roundoff (-1) ?

I've revised the entire routine - it should do what you want (although I don't know what values you are feeding it) - So now it will work like this:


Doing 4 digits:

Rounding (123450001, 4, 2)

Result:

12345.00


Doing 2 digits:

Rounding (123450001, 2, 2)

Result:

1234500.01


Doing 4 digits (increments if > .5)

Rounding (876512345678, 8, 4)

Result:

8765.1235

Revised simplified function that should do everything you are asking:

Public Function Rounding(NumberValue, DecimalPoints, RoundOff )

    RealNumber = Mid(NumberValue, 1, Len(NumberValue)-DecimalPoints)
    DecimalNumber = Round("." & Right(NumberValue,DecimalPoints), RoundOff)
    Rounding = FormatNumber(RealNumber + DecimalNumber,RoundOff,,,0)

End Function

Here's a working version of your Function:

Public Function Rounding(NumberValue, DecimalPoints, RoundOff)

    RealNumber=left(NumberValue,Len(NumberValue)-DecimalPoints)
    DecimalNumber="." & Right(NumberValue,DecimalPoints)
    NumberValue = RealNumber + DecimalNumber
    NumberValue = Round(NumberValue,RoundOff)
    Rounding = FormatNumber(NumberValue, RoundOff,,,0)

End Function

OTHER TIPS

I'm pretty sure you won't be able to use the Round() function for what you need. Take a look at the FormatNumber() or FormatCurrency() functions as they have the option to "IncludeLeadingZero".

Take a look at the answer from the following link for more information:

vbscript round to 2 decimal places using Ccur

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top