Input:
"HRA 1000000 and TDA 120000 and other benefits"
OutPut required is:
"HRA 1,000,000.00 and TDA 120,000.00 and other benefits"
string input = "HRA 1000000 and TDA 120000 and other benefits";
string[] numbers = Regex.Split(input, @"\D+");
foreach (string value in numbers)
{
if (!string.IsNullOrEmpty(value))
{
string i = (value);
i = String.Format("{0:n}", decimal.Parse(i, System.Globalization.NumberStyles.Any));
}
}
Check my code I search for the numbers in string and replace its by the new format
string strRegex = @"(\d+)";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"HRA 1000000 and TDA 120000 and other benefits";
foreach(Match match in myRegex.Matches(strTargetString))
{
strTargetString = strTargetString.Replace(match.Value,
string.Format("{0:n}", decimal.Parse(match.Value, System.Globalization.NumberStyles.Any)));
}
You could do this. Test the value
if a decimal and replace it in the original string.
foreach (string value in numbers)
{
decimal number = 0;
decimal.TryParse(value, out number);
if (number > 0)
{
input = input.Replace(value, string.Format("{0:N0}", number));
}
}
String.Format("{0:0,0.00}", 12345); // "12,345.00"
似乎是你所需要的。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.