I'm trying to manipulate a string..
For example:
string DiagnosesString "250.00 03 350.0001 450.00 01 550.00 02";
Output
250.00
350.00
450.00
550.00
I tried to use a Split metho开发者_JS百科d. As you can see on my example in 350.0001 and its output is 350.00.
I can removed 03 , 02 , etc..
Question: 1.) How I will removed the o1 of 350.0001 get only 350.00?
here's my code:
string DiagnosisCodestemp = DiagnosesString.Replace(" 01 ", " ").Replace(" 02 ", " ").Replace(" 03 ", " ").Replace(" 04 ", " ").Replace(" 05 ", " ").Replace(" 06 ", " ").Replace(" 07 ", " ");
string[] DiagnosisCodesParts = DiagnosisCodestemp.Split();
var DiagnosisCodesParts =
DiagnosesString.Split(' ')
.Where(s => !s.StartsWith("0"))
.Select(s => Decimal.Parse(s))
.Select(d => d.ToString("0.00");
How about doing a Decimal.TryParse and format the decimal numbers. THis will also weed out the bad values.
string DiagnosesString = "250.00 03 350.0001 450.00 01 550.00 02";
var diagnoses = DiagnosesString.Split(' ');
foreach(var diagnosis in diagnoses)
{
decimal temp;
if(diagnosis.Contains(".") && Decimal.TryParse(diagnosis, out temp))
{
// do something
var value = temp.ToString("0.00");
}
}
If you are dealing with numeric values, I would recommend parsing them into actual numeric types, e.g. using Decimal.Parse
or Decimal.TryParse
. You can then use formatting to convert it back to a string, e.g. myNumber.ToString("N2")
You can parse them as a double. I like to use XmlConvert
for this:
foreach (var s in diagnosisCodesParts)
{
var d = XmlConvert.ToDouble(s);
Console.WriteLine(string.Format({0:2}, d));
}
List list = new List();
string DiagnosesString = "250.00 03 350.0001 450.00 01 550.00 02";
string DiagnosisCodestemp = DiagnosesString.Replace(" 01 ", " ").Replace(" 02 ", " ").Replace(" 03 ", " ").Replace(" 04 ", " ").Replace(" 05 ", " ").Replace(" 06 ", " ").Replace(" 07 ", " ");
string[] DiagnosisCodesParts = DiagnosisCodestemp.Split();
foreach (var item in DiagnosisCodesParts)
{
string[] parts = item.Split('.');
string num = parts[1];
string finalValue = string.Empty;
if (num.Length > 2)
{
num = num.Replace(num, "00");
finalValue = parts[0]+"."+num;
}
else
{
finalValue = parts[0] + "." + num;
}
list.Add(finalValue);
}
o/p : 250.00 350.00 450.00 550.00
Try aplitting on the space.
string DiagnosisCodestemp = DiagnosesString.Replace(" 01 ", " ").Replace(" 02 ", " ").Replace(" 03 ", " ").Replace(" 04 ", " ").Replace(" 05 ", " ").Replace(" 06 ", " ").Replace(" 07 ", " ");
string[] DiagnosisCodesParts = DiagnosisCodestemp.Split(' ');
This should produce an array with values and you can filter for the ones you need. You can then convert the values to decimal and round to 2 places to remove the extra values.
1) convert the string to a decimal 2) round the decimal to 2 decimal places 3) convert it back to a string
string DiagnosesString = "250.00 03 350.0001 450.00 01 550.00 02";
string DiagnosisCodestemp = DiagnosesString.Replace(" 01 ", " ").Replace(" 02 ", " ").Replace(" 03 ", " ").Replace(" 04 ", " ").Replace(" 05 ", " ").Replace(" 06 ", " ").Replace(" 07 ", " ");
string[] DiagnosisCodesParts = DiagnosisCodestemp.Split();
foreach (var item in DiagnosisCodesParts)
{
string[] parts = item.Split('.');
string num = parts[1];
if (num.Length > 2)
{
num = num.Replace(num, "00");
}
}
Ok, I'm not sure I completely understood your question, but I'll try.
- You split as you've done before.
- You filter/ignore all strings with a length of 2 or less.
- You convert the strings left in your array to double.
- You round those values to 2 numbers after the point.
You will need to convert the string to a numerical value, I choose decimal as it does not lose precision like double or float. Then you can use the format string specifier to convert it to the decimal form you want:
var list = List<string>();
foreach(var s in splitString) {
// code to rule out 02, 01, etc. goes here
decimal tmp;
if (decimal.TryParse(s, out tmp)) {
var code = tmp.ToString("0.00");
list.Add(code);
} else {
// something went wrong, handle it here
}
}
you could match the numbers using a regex
/\d+\.\d+/
but it depends whether you can define your number more precisely or not.
decimal number;
string DiagnosesString = "250.00 03 350.0001 450.00 01 550.00 02";
foreach(var num in Regex.Matches(DiagnosesString, @"\d+\.\d+"))
{
Decimal.TryParse(num.ToString(), out number);
Console.WriteLine(number.ToString("0.00"));
}
probably you even need a regex like this:
/(\d+.\d\d)\d*/
to match only numbers in the format 0.00... you should provide more data how you want your data to be parsed..
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string input = "250.00 03 350.0001 450.00 01 550.00 02";
Match match = Regex.Match(input, @"[0-9]+[/.][0-9]+", RegexOptions.IgnoreCase);
while (match.Success)
{
string key = match.Value;
Console.WriteLine(String.Format("{0:0.00}", Convert.ToDecimal(key, new CultureInfo("en-US"))));
match = match.NextMatch();
}
}
}
精彩评论