How to format Currency string for Integer?

I have a line with a currency format, for example, $ 35.00, and this needs to be converted to 35.

Is it possible to get with String.Format{ }

+5
source share
1 answer
int value = int.Parse("$35.00", NumberStyles.Currency);

Should give you the answer you need.

However, a value of $ 35.50 converted to an integer will most likely not return what you want, since integers do not support partial (decimal) numbers. You did not indicate what to expect in this situation.

[EDIT: changed from double to decimal, which is safer to use with currency]

If you want to get a value of 35.5 in this situation, you can use the decimal type.

decimal value = decimal.Parse("$35.00", NumberStyles.Currency);

, .

+12

All Articles