LINQ方式将包含小数的文本转换为int? [英] A LINQ way of converting text containing decimals to int?

查看:91
本文介绍了LINQ方式将包含小数的文本转换为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考Int32.TryParse方法: http:// msdn.microsoft.com/en-us/library/zf50za27.aspx

如果必须,特别是这个例子:


numericString = " 10345.72"

styles = NumberStyles.Integer或NumberStyles.AllowDecimalPoint

CallTryParse(numericString,styles)


哪个失败转换字符串10345.72到了int 10345,

,因为。之后的额外数字。


我的问题是:有一个整洁的LINQ吗?类型解析这样的方式

一个字符串,以便第一次。 (句号)被找到,字符串是

提取,然后我可以使用Int32.TryParse?


我知道我可以使用一个字符来解决这个问题缓冲区,读取每个
Unicode字节进入缓冲区,然后在我第一次找到一个

。时停止,然后读出缓冲区来创建一个字符串,然后使用

Int32.TryParse从字符串中提取int。也就是说,也许我可以使用StringBuilder方法将CopyTo变为char数组,然后使用

技巧在。之前提取数字字符串。 />

例如 -


StringBuilder sb = new StringBuilder(" 10345.72");

int count = sb.Length;


char [] temporary_charBuf1 = new char [100]; // 1 of 2


sb.Copy(0,temporary_charBuf1,0,count);


char [] charBuf2 = new char [ 100] // 2 of 2

foreach(charc in temporary_charBuf){//此处代码:如果char不是

期间,请填写charBuf2,否则不会在一段时间后中断}


string numericString ="" ;;


numericString = new string(charBuf2); //创建新的字符串传递

charBuf2到构造函数中


" 10345.72。


但我正在寻找一种更快或更短的方式(因为这不是一笔很大的b
交易,而且我不是''我想花太多代码来制作

工作)。正如你从上面所看到的,它甚至不包括foreach循环中的

代码,它是很多代码。


我们可以用一行LINQ替换它(加上我正在努力学习

LINQ,因为我去)?

没什么大不了的(伪代码没问题)......好奇。


RL

Refer to Int32.TryParse method: http://msdn.microsoft.com/en-us/library/zf50za27.aspx
if you have to, specifically this example:

numericString = "10345.72"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)

Which failed to convert the string "10345.72" to the int 10345,
because of the extra digits after the "."

My question is this: is there a neat "LINQ" type way of parsing such
a string so that the first time a "." (period) is found, the string is
extracted, and then I can use Int32.TryParse?

I know I can solve this problem using a character buffer, reading each
Unicode byte into the buffer, then stopping the first time I find a
".", then reading out the buffer to create a string, then using
Int32.TryParse to extract the int from the string. That is, perhaps I
can use the StringBuilder method to CopyTo a char array, then use a
trick to extract the numeric string before the "."

For example--

StringBuilder sb = new StringBuilder ("10345.72");
int count = sb.Length;

char[] temporary_charBuf1 = new char[100]; // 1 of 2

sb.Copy(0,temporary_charBuf1,0,count);

char[] charBuf2 = new char [100] //2 of 2

foreach (char c in temporary_charBuf) {//code here: if char is not a
period, fill charBuf2, otherwise not and break once a period is found}

string numericString = "";

numericString = new string(charBuf2); // Create new string passing
charBuf2 into the constructor

Now your numericString should be 10345 from the original string
"10345.72".

But I''m looking for a faster or shorter way (since this is not a big
deal, and I don''t want to spend too many lines of code making it
work). As you can see from the above, which does not even include the
code inside the foreach loop, it''s a lot of code.

Can we replace this with one line of LINQ (plus I''m trying to learn
LINQ as I go)?
No big deal (pseudo code is OK)...just curious.

RL

推荐答案

raylopez99< ra ******** @ yahoo.comwrote:


< snip>
raylopez99 <ra********@yahoo.comwrote:

<snip>

我们可以用一行LINQ替换它(加上我''我试着学习

LINQ,因为我去了)?

没什么大不了的(伪代码还可以)......只是好奇。
Can we replace this with one line of LINQ (plus I''m trying to learn
LINQ as I go)?
No big deal (pseudo code is OK)...just curious.



你*可以*用LINQ来做,但是它会非常缓慢且

难以理解。这是一个更简单的方法:


public int ParseInt32BeforePeriod(string text)

{

int periodIndex = text .IndexOf(''。'');

if(periodIndex!= -1)

{

text = text.Substring(0 ,periodIndex);

}

返回int.Parse(文字);

}


(如果你愿意的话,你可以把它转换成一个试用了
参数的TryParseInt32BeforePeriod并很容易地返回一个布尔值。)


-

Jon Skeet - < sk *** @ pobox.com>

网站: http://www.pobox.com/~skeet

Blog: http://www.msmvps.com/jon.skeet

C#深度: http://csharpindepth.com

You *could* do it with LINQ, but it would be painfully slow and
unreadable. Here''s a simpler method to do it:

public int ParseInt32BeforePeriod(string text)
{
int periodIndex = text.IndexOf(''.'');
if (periodIndex != -1)
{
text = text.Substring(0, periodIndex);
}
return int.Parse(text);
}

(You can turn it into a TryParseInt32BeforePeriod taking an out
parameter and returning a boolean pretty easily, if you want.)

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com


raylopez99写道:
raylopez99 wrote:

参考Int32.TryParse方法: http://msdn.microsoft.com/en-us/library/zf50za27.aspx

如果必须,特别是这个例子:


numericString =" 10345.72"

styles = NumberStyles.Integer或NumberStyles.AllowDecimalPoint

CallTryParse(numericString,styles)


未能转换字符串10345.72到了int 10345,

,因为。之后的额外数字。


我的问题是:有一个整洁的LINQ吗?类型解析这样的方式

一个字符串,以便第一次。 (句号)被找到,字符串是

提取,然后我可以使用Int32.TryParse?
Refer to Int32.TryParse method: http://msdn.microsoft.com/en-us/library/zf50za27.aspx
if you have to, specifically this example:

numericString = "10345.72"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)

Which failed to convert the string "10345.72" to the int 10345,
because of the extra digits after the "."

My question is this: is there a neat "LINQ" type way of parsing such
a string so that the first time a "." (period) is found, the string is
extracted, and then I can use Int32.TryParse?



int result =(int)Math.Floor(decimal.Parse(" 10345.72"));


Alun Harford

int result = (int)Math.Floor(decimal.Parse("10345.72"));

Alun Harford





Alun Harford写道:


Alun Harford wrote:

> ;

int result =(int)Math.Floor(decimal.Parse(" 10345.72"));
>
int result = (int)Math.Floor(decimal.Parse("10345.72"));



Cripes,is那合法吗?我的gawd,是的。那太棒了。


RL

Cripes, is that legal? My gawd, it is. That''s brilliant.

RL


这篇关于LINQ方式将包含小数的文本转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆