2017-06-19 53 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