将字符串转换为Int32 [英] Converting String To Int32

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

问题描述

大家好,



这是一个简单的问题:如何将此字符串123,45转换为此int 12345?

但是同样的方法应该将123,445转换为此int 12345或123,40转换为此int 12340.

我希望您能看到它背后的系统。我也不想在那个方法中使用float,double或decimal,因为我想要非常大的数字的高精度(例如这个字符串123456789,0123到12345678901)

Hello folks,

here's a quick question: How can I convert this string "123,45" to this int 12345?
But the same method should convert "123,445" to this int 12345 or "123,40" to this int 12340.
I hope you can see the system behind it. I also don't want to use float, double or decimals in that method as I want high precision on very big numbers (for example this string "123456789,0123" to 12345678901)

推荐答案

首先,此操作称为解析。当你尝试将字符串解释为数字的表示时,就会发生这种情况。



这是如何:

First of all, this operation is called parsing. This is what really happens when you try to interpret a string as a representation of a number.

This is how:
string input = //...
int value = int.Parse(input, System.Globalization.NumberStyles.AllowThousands);






or

string input = //...
int value;
bool success = int.TryParse(input, System.Globalization.NumberStyles.AllowThousands, null, out value);





请参阅:

http://msdn.microsoft.com/en-us/library/c09yxbyt.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/zf50za27.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx [ ^ ]。



第一个变体可能会抛出异常,因此您可能需要处理它并让用户进行更改以输入正确的值。在第二个示例中,您可以执行相同操作而不使用任何异常,但是您需要检查调用的结果。



格式提供程序的空值意味着使用当前的文化。如果','作为数千或数百个分隔符,这很重要,因为其他一些文化使用'。'(或其他东西),这种格式依赖于文化,所以它不适用于所有情况。你总是想要',',你应该指定一些固定的文化,比如新的 System.Globalization.CultureInfo(en-US,false); 请看:

http://msdn.microsoft.com/en- us / library / system.globalization.cultureinfo.aspx [ ^ ]。



-SA



Please see:
http://msdn.microsoft.com/en-us/library/c09yxbyt.aspx[^],
http://msdn.microsoft.com/en-us/library/zf50za27.aspx[^],
http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx[^].

First variant may throw exception, so you may need to handle it and let a user a change to input correct value. In the second sample, you can do the same without using any exception, but you need to check up the result of the call.

The null value for format provider means using the current culture. In case of ',' as a thousands or hundreds separator, this is important, because some other cultures use '.' (or something else), to this format is culture-dependent, so it won't work in all cases. In you always want ',', you should rather specify some fixed culture, such as new System.Globalization.CultureInfo("en-US", false); please see:
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx[^].

—SA


你可以使用 Convert.ToInt32 ,它需要特殊数字字符,如'+'' - '','等等

you can use Convert.ToInt32, it takes special number chars like '+' '-' ',' etc
// This will parse +2,147,483,647 as 2147483647 
Convert.ToInt32(input);





如果你有一个有趣的字符串输入并且想要删除所有非数字字符,你可以使用正则表达式:



If you have a funny string input and you want to remove all the non-numeric chars, you can use Regex:

// This regex will remove any char that is not a number or '+' or '-'
Regex rgx = new Regex("[0-9+-]");
str = rgx.Replace(str, "");



最好,

H


Best,
H


private static int ToInt(string t)
        {
            string[] spl = t.Split(new char[]{',', '.'});
            int f = Convert.ToInt32(spl[0]) * 100;
            double d = Convert.ToDouble("0," + spl[1]);
            d = Math.Round(d, 2);
            if (d >= 1.0d)
            {
                f += 100;
                d -= 1.0d;
            }
            f += Convert.ToInt32(d * 100);
            return f;
        }



这个oe应该有效。


This oe should work.


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

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