Sambalado
Sambalado

Reputation: 33

convert non decimal string to specific decimal number in vb

So I have this :

dim nonDecString as string = "12"
dim decimalPlaces as integer = 2 //this value can be changed dynamically

What I want is to convert that nonDecString to have decimal places as "12.00" or "12.000", or "12.00n0".

Upvotes: 0

Views: 210

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460038

You can use Decimal.Parse and Decimal.ToString:

Dim dec As Decimal = Decimal.Parse(nonDecString)
Dim result = dec.ToString("N" & decimalPlaces)

Read: Standard Numeric Format Strings, The numeric ("N") format specifier

The precision specifier indicates the desired number of digits after the decimal point. If the precision specifier is omitted, the number of decimal places is defined by the current NumberFormatInfo.NumberDecimalDigits property.

Upvotes: 1

Related Questions