1

I just want to know how to total formatted strings like $ 100,000.00 Ive use a split method but it doesnt work when to many "," are there..

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Prince Jea
  • 5,524
  • 7
  • 28
  • 46

4 Answers4

1

You need to get a hold of them before they have been formatted. Trying to perform computations on numbers after they have been formatted is retarded, and highly error prone. Especially if your app is ever used under a different locale, where the thousands separator and the decimal point characters are not what you expected them to be.

Furthermore, be advised that the String.Split method will work just fine regardless of how many "," are there, so your problem is not the split method, it is something else in your code.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
0
decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html);

Outputs the following:

Order Total: $1,921.39

or perhaps

String.Format("{0:#,###}", bigNumber);
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 2
    I think he wants to revert the process to get back the original number –  Jan 12 '12 at 14:13
0

You can use the NumberStyles enumeration overload of the parse method in your example. e.g.

string money1 = "$100,000.00";
string money2 = "$300,500.00";
Double unformattedMoney = Double.Parse(money1, System.Globalization.NumberStyles.Any);
Double unformattedMoney2 = Double.Parse(money2, System.Globalization.NumberStyles.Any);

Console.WriteLine(unformattedMoney + unformattedMoney2);

As a side note, this doesn't account for incorrect numbers and will throw an error if you attempt to format a bad string.

George Johnston
  • 31,652
  • 27
  • 127
  • 172
0

if u are talking about currency specifically, check this Convert any currency string to double

the idea is to use NumberStyle class. Hope this helps

Community
  • 1
  • 1
Boomer
  • 1,468
  • 1
  • 15
  • 19