Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串0.00增加x%_C#_String - Fatal编程技术网

C# 将字符串0.00增加x%

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 -

我需要将“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 - 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%  
}