2017-06-19 91 views
0

在VS2017 VB.net webform應用程序中,我試圖格式化一個數字,這是一個字符串(「12345.0000」),格式爲:「12345.00」,即2位小數沒有千位分隔符。爲此,我使用以下代碼行: -VS2017 VB.NET格式字符串不工作

rentalPriceVal = Format(memberPrices.RentalPrice, "0.00") 

它返回「0.00」。

我原本有FormatNumber(memberPrices.RentalPrice, 2)但增加了一個依賴於區域的千位分隔符和十進制標識符。

我也試過:memberPrices.RentalPrice.ToString("0.00"),但是錯誤「無法將字符串轉換爲IFormatProvider」。

我不知所措。我弄亂了語法嗎,有沒有更好的方式,我還沒有遇到或者是不可能的?

謝謝。

+3

將字符串轉換爲數字,然後格式化。 – GSerg

+0

'格式(Val(memberPrices.RentalPrice),「0.00」)' – Slai

回答

2

使用Decimal.TryParse()字符串轉換爲十進制,然後用Decimal.ToString()與 「F2」 作爲format

Dim price As Decimal 
If Decimal.TryParse(memberprices.RentalPrice, price) Then 
    Dim strPrice As String = price.ToString("F2") 
    ' .. use "strPrice" somehow ... 
    Debug.Print(strPrice) 
End If