2

I Have a field that should consist a currency, which is according to my Region is Indonesia which is IDR or Rp. and i build it with string.format like this :

 Dim price As Single = Single.Parse(Convert.ToSingle(lblAmountGV.Text))
 lblAmount.Text = String.Format("{0,C}", price)

but it give me a dollar sign. and i Change the code :

 lblAmount.Text = String.Format("Rp{0}", price)

but i didn't get the dot (.) and Comma(,) . so I change the code again by using FormatCurrency :

 lblAmount.Text = FormatCurrency(lblAmountGV.Text, , , TriState.True, TriState.True)

but it still give me a Dollar sign, later i found how to change the CultureInfo :

by imports :

Imports System.Globalization

and on my code :

 Dim culture = CultureInfo.GetCultureInfo(" id-ID")
    Dim format = DirectCast(culture.NumberFormat.Clone(), NumberFormatInfo)
    format.CurrencySymbol = "Rp."
    var number = Decimal.Parse(lblAmountGV.Text, NumberStyles.Currency, format);

lblAmount.Text = number

but it still give me an $ sign, how to change the $ programatically?

4

1 回答 1

3

我发现您发布的内容存在一些问题-

此行不正确

String.Format("{0,C}", price)

您需要使用冒号来添加其他格式参数。如果你想用小数位格式化货币,你还需要指出小数位数。它应该是这样的

String.Format("{0:C2}", price)

这条线有一个额外的空间,导致它失败CultureNotFoundException

CultureInfo.GetCultureInfo(" id-ID")

应该

CultureInfo.GetCultureInfo("id-ID")

这段代码对我有用:

Dim culture As CultureInfo = CultureInfo.GetCultureInfo("id-ID")
Dim price As Double = 10.05

Dim result As String = String.Format(culture, "{0:C2}", price)

你可以在这里看到它的作用

如果您熟悉LINQPad,您可以将以下内容粘贴到 LINQPad 中并查看不带美元符号的正确格式 -

String.Format(CultureInfo.GetCultureInfo("id-ID"), "{0:C2}", 10.05).Dump()
于 2013-05-28T02:33:37.647 回答