将字符串变量转换为数字变量 [英] Convert String Variable to number variable

查看:159
本文介绍了将字符串变量转换为数字变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


i有一个字符串变量,在这个字符串中我有一个像这样的数字2.0.0.0

现在我想将它转换为数字变量,我试过但我无法转换它,

如何将其转换为数字?

Hi i have a string variable and in this string i have a number like this "2.0.0.0"
and now i want to convert it to number variable, i tried but i couldn't Convert it,
how can i Convert it to number?

推荐答案

如果你删除'。',你可以考虑s,也许使用string.Replace(。,)然后,每个位置代表数千,数百,数十和单位 - 然后转换字符串与'。的删除为整数 - 没有双关语意,但我不是确定真正给你的价值,将其存储为数字
you could consider that if you remove the '.'s, maybe using string.Replace(".","") then, each position represents thousands, hundreds, tens and units - then convert the string with the '.'s remove to an integer - no pun intended, but Im not sure what value that really gives you, storing it as a number


否(现有)数字格式可以接受它。浮点数也只允许一个小数点。您的号码包含4.



从字符串中删除,然后将其转换。或者至少在字符串中只允许1个小数点。



如果您有一个小数点,您仍然可以将其转换为 Double [ ^ ], Float [ ^ ]等号码。即使你有一个小数点,你必须将它转换为十进制或浮点数,转换为一个小数点的整数将导致错误。



有一种更好的方法可以将字符串转换为整数,并使用TryParse [ ^ ]在您的代码中,如果转换了数字,则返回true;如果有,则返回false格式中的任何错误。



记住数据类型的大小



即使将字符串转换为数字数据类型,结果也将基于数据类型的大小。您需要考虑要使用的数据类型的大小。



http ://msdn.microsoft.com/en-us/library/ms228360(v = vs.90).aspx [ ^ ]
No (existing) number format can accept it. Floating point numbers would also allow only one decimal point. Your number contains 4.

Remove the . from your string, and then convert it. Or atleast allow only 1 decimal point in your string.

If you're having one decimal point, you can still convert it to Double[^], Float[^] etc numbers. Even if you have one decimal point, you will have to convert it to decimal or float, conversion to an integer with a single decimal point would cause the error.

There is a better way of converting the strings to integers and getting to know whether they can be converted or not, using a TryParse[^] in your code, which would either return a true if number was converted or a false if there were any errors in the format.

Remember the size of the data type

Even if you convert the string to a numeric data type, the result would be based on the size of the data type. You need to consider the size of the data type you're going to use.

http://msdn.microsoft.com/en-us/library/ms228360(v=vs.90).aspx[^]


这篇关于将字符串变量转换为数字变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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