从字符串中提取数字
我正在尝试操作一个字符串。
例如:
string DiagnosesString "250.00 03 350.0001 450.00 01 550.00 02";
输出
250.00
350.00
450.00
550.00
我尝试使用 Split 方法。正如您在 350.0001 中的示例中看到的,其输出是 350.00。
我可以删除 03 、 02 等。
问题: 1.) 我如何删除 350.0001 的 o1 只得到 350.00?
这是我的代码:
string DiagnosisCodestemp = DiagnosesString.Replace(" 01 ", " ").Replace(" 02 ", " ").Replace(" 03 ", " ").Replace(" 04 ", " ").Replace(" 05 ", " ").Replace(" 06 ", " ").Replace(" 07 ", " ");
string[] DiagnosisCodesParts = DiagnosisCodestemp.Split();
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 method. 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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
如何执行 Decimal.TryParse 并格式化十进制数字。这也将清除不良价值观。
How about doing a Decimal.TryParse and format the decimal numbers. THis will also weed out the bad values.
如果您正在处理数值,我建议将它们解析为实际的数值类型,例如使用
Decimal.Parse
或Decimal.TryParse
。然后,您可以使用格式将其转换回字符串,例如myNumber.ToString("N2")
If you are dealing with numeric values, I would recommend parsing them into actual numeric types, e.g. using
Decimal.Parse
orDecimal.TryParse
. You can then use formatting to convert it back to a string, e.g.myNumber.ToString("N2")
您可以将它们解析为双精度。我喜欢使用
XmlConvert
来实现此目的:You can parse them as a double. I like to use
XmlConvert
for this:列表列表=新列表();
输出:250.00 350.00 450.00 550.00
List list = new List();
o/p : 250.00 350.00 450.00 550.00
尝试在空间上进行分割。
这应该会生成一个包含值的数组,您可以过滤所需的值。然后,您可以将这些值转换为小数并四舍五入到 2 位以删除多余的值。
Try aplitting on the space.
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)将字符串转换为十进制
2) 将小数点四舍五入至小数点后 2 位
3)将其转换回字符串
1) convert the string to a decimal
2) round the decimal to 2 decimal places
3) convert it back to a 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" ,“ ”)。替换(“ 06 ”,“ ”)。替换(“ 07 ”,“ ”);
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 ", " ");
好吧,我不确定我完全理解你的问题,但我会尝试。
Ok, I'm not sure I completely understood your question, but I'll try.
您需要将字符串转换为数值,我选择十进制,因为它不会像双精度或浮点那样丢失精度。然后您可以使用格式字符串说明符将其转换为您想要的十进制形式:
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:
您可以使用正则表达式来匹配数字,
但这取决于您是否可以更精确地定义您的数字。
也许您甚至需要这样的正则表达式:
仅匹配格式为 0.00 的数字...您应该提供更多您希望如何解析数据的数据..
you could match the numbers using a regex
but it depends whether you can define your number more precisely or not.
probably you even need a regex like this:
to match only numbers in the format 0.00... you should provide more data how you want your data to be parsed..