In C#, I'm trying to convert a string to decimal.
For example, the string is "(USD 92.90)"
How would you parse this out as a decimal with Decimal.Parse fcn.
The unary plus operator ( + ) will convert a string into a number. The operator will go before the operand. We can also use the unary plus operator ( + ) to convert a string into a floating point number.
It's not possible to convert an empty string to a number (decimal or not). You can test for an empty string before trying the conversion or use decimal. TryParse() - I think that was available in 1.1.
literal_eval() function to convert the hexadecimal string to decimal string. convertion = literal_eval(testing_string) # At last, print result. print ("The converted hexadecimal string into decimal string is: " + str(convertion))
ToString() is a method in C# that is called on decimal values. It converts a numeric value that is a decimal to a string.
I'm going on the assumption here that the string you're trying to parse is an actual currency value.
CultureInfo c = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
c.NumberFormat.CurrencyNegativePattern = 14; // From MSDN -- no enum values for this
c.NumberFormat.CurrencySymbol = "USD";
decimal d = Decimal.Parse("(USD 92.90)", NumberStyles.Currency, c);
You could start off with a reg-exp to extract the number part and then use Decimal.TryParse to parse the sub-string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With