C# 将字符串0.00增加x%
我需要将“0.3332 USD”、“12.5334 USD”、“6.2222 USD”等字符串增加x%,如何将这些字符串转换为数字格式xxx.xx示例“0.33”、“12.53”、“6.22” 我试过这个,但它的工作方法C# 将字符串0.00增加x%,c#,string,C#,String,我需要将“0.3332 USD”、“12.5334 USD”、“6.2222 USD”等字符串增加x%,如何将这些字符串转换为数字格式xxx.xx示例“0.33”、“12.53”、“6.22” 我试过这个,但它的工作方法 字符串cmprice=result.Substring(0,x) 如果可以保证字符串始终以“USD”结尾,则可以创建一个比完整字符串短四个字符的子字符串,然后对其进行解析 String cmprice = result.Substring(0, result.Length -
字符串cmprice=result.Substring(0,x) 如果可以保证字符串始终以“USD”结尾,则可以创建一个比完整字符串短四个字符的子字符串,然后对其进行解析
String cmprice = result.Substring(0, result.Length - 4);
decimal parsedResult;
decimal.TryParse(cmprice, out parsedResult);
您可以将
decimal.TryParse
与正确的NumberFormatInfo
一起使用:
string dollars = "0.3332 USD";
decimal price;
var nfi = (NumberFormatInfo)NumberFormatInfo.InvariantInfo.Clone();
nfi.CurrencySymbol = "USD";
bool validFormat = decimal.TryParse(dollars, NumberStyles.Currency, nfi, out price);
if (validFormat)
{
// apply your percent logic, f.e.:
decimal newPrice = price + (price * 0.15m); // + 15%
}