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..
-
Let me understand: you have a string like that and you need to get the int amount? – Marco Jan 12 '12 at 14:14
-
1If it's "solved", you should click the hollow checkmark next to the answer that was most helpful to you in solving the problem, not add `[solved]` to the title. – Cody Gray - on strike Jan 12 '12 at 15:32
-
Sorry Im just new here... I am used to forums :laughs – Prince Jea Jan 12 '12 at 15:34
4 Answers
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.

- 56,297
- 11
- 110
- 142
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);

- 36,338
- 80
- 323
- 498
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.

- 31,652
- 27
- 127
- 172
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