Bradley Oesch
Bradley Oesch

Reputation: 763

How can I specify an integer's total number of digits in VBScript?

I have a "text field" in SSRS in Visual Studio 2008 that I would like to convert to a number in order for the export to Excel to work. Simply changing the properties to Number does not work. So I've decided to try to write some code in the Value field for it to work.

I have an invoice number that must always have seven digits, e.g. 1938576, 0000001. Using CInt(Fields!Invoice.Value) does not work as it does not keep any leading zeroes. How can I convert this field to a numerical value (integer for this one, but I have a couple other fields that it must work with doubles) while also determining exactly how many digits must be shown? Something like CInt(Fields!Invoice.Value, 7) would be great, but I don't think that parameter is part of the function.

Upvotes: 0

Views: 2986

Answers (1)

Tmdean
Tmdean

Reputation: 9309

VBScript doesn't have any number formatting functions that will work for this. You have to write your own.

Function PadNumber(x, digit_count)
    If Len(x) < digit_count Then
        PadNumber = String(digit_count - Len(x), "0") & x
    Else
        PadNumber = x
    End If
End Function

Upvotes: 1

Related Questions