将非十进制字符串转换为 vb 中的特定十进制数

convert non decimal string to specific decimal number in vb

所以我有这个:

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

我想要的是将该 nonDecString 转换为具有小数位的“12.00”或“12.000”或“12.00n0”。

您可以使用 Decimal.ParseDecimal.ToString:

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

阅读: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.