huMpty duMpty
huMpty duMpty

Reputation: 14460

Convert currency with symbol to decimal

I have a text box which displays the currency values like 250.00 €,$1,000.00 ,£100.00 etc

I have culture created based on the currency symbol.

  var culture = new CultureInfo(symbol); //symbol may be en-US,en-GB etc..
  culture.NumberFormat.CurrencyDecimalSeparator = ".";

and doing something like below at the moment

decimal amount;    
decimal.TryParse(txtAmount.Text, NumberStyles.Any, _culture, out rate);

which works fine.

I have issue with Swedish Kroner when the amount is >1000 as it formats the currency.

i.e.

when enter 100 it become 100.00 kr and works fine

when enter 1000 it 1.000.00 kr and it get failed and amount become 0

Any idea of getting around this?

Upvotes: 2

Views: 334

Answers (2)

huMpty duMpty
huMpty duMpty

Reputation: 14460

It was all about creating the culture.

As DanielGimenez pointed out, I removed the NumberFormat.CurrencyDecimalSeparator = "." and all works fine

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172418

You can set the culture like this:-

CultureInfo sw= new CultureInfo("sv-SE");
sw = (CultureInfo)sw.Clone();
sw.NumberFormat.CurrencyPositivePattern = 3;
sw.NumberFormat.CurrencyNegativePattern = 3;

and then

var rate = 123.45M;
txtAmount.Text = rate.ToString("C", sw);

Output

123,45 kr

Upvotes: 1

Related Questions