简体   繁体   English

如何在C#中将浮点字符串转换为int

[英]How do i convert a floating point string to an int in c#

I have a string: 我有一个字符串:

string test = "19,95"; 字符串测试=“ 19,95”;

and I wan't to convert it to an int. 并且我不想将其转换为int。

I use: 我用:

int num = Convert.Int32(test); int num = Convert.Int32(测试);

However it throws an FormatException. 但是,它将引发FormatException。 And tells me that the string is not a proper string for conversion. 并告诉我该字符串不是正确的转换字符串。

My guess is that it has to do with the decimal seperator. 我的猜测是它与十进制分隔符有关。

How do i work around this? 我该如何解决?

using System.Globalization;
...
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
double num = Convert.ToDouble(test, (IFormatProvider)nfi);

Tested & working. 经过测试和工作。

For completeness: 为了完整性:

int applesApplesApples = Math.Ceiling(num);
int bananaBananaBanana = (int)num;
int cucumberCucumberCucumber = Math.Floor(num);

[Update] [更新]

As Rushyo correctly pointed out in my comments below, this example is contrived and the best practice approach is to identify which culture you are working with in order to get the correct CultureInfo object to work off of. 正如Rushyo在下面的评论中正确指出的那样,该示例是人为设计的,最佳实践方法是确定要使用的文化,以便使用正确的CultureInfo对象。

You can then utilize the localized NumberFormatInfo from that specific CultureInfo when doing all your numeric formatting. 然后,在执行所有数字格式设置时,可以使用该特定CultureInfo中的本地化NumberFormatInfo。

string dblText = "19,95";
CultureInfo ci = new CultureInfo ("en-US", true);
ci.NumberFormat.NumberDecimalSeparator = ",";
double dblValue = double.Parse (dblText, NumberStyles.AllowDecimalPoint, ci);

If you need 19 (casting off the decimal part): 如果需要19(舍去小数部分):

int intValue = (int)dblValue;

If you need 20 (mathematical rounding): 如果需要20(数学舍入):

int intValue = (int)(dblValue + 0.5);

What integer would you expect to get from "19,95" ? 您期望从"19,95"得到什么整数? 1995? 1995年? 19? 19? 20? 20吗

Perhaps you should convert the number to a double first, and then round or truncate in whichever direction makes sense for your application. 也许您应该先将数字转换为双精度,然后再对应用程序有意义的任何方向四舍五入或截断。

As the others already mentioned the main problem of your question is, that you don't gave any information about what result you expect and some also brought up the problem with the Culture (decimal separator, thousand separator). 正如其他人已经提到的那样,您问题的主要问题是,您没有提供有关期望结果的任何信息,还有一些人也提出了Culture(十进制分隔符,千位分隔符)问题。 So i will make a little sum up: 因此,我将作一点总结:

private int Parse(string text)
{
    Decimal value;

    //Select the culture you like to use
    var culture = CultureInfo.CurrentCulture;
    //var culture = CultureInfo.GetCultureInfo("en-US");
    //var culture = CultureInfo.GetCultureInfo("de-DE");

    if (Decimal.TryParse(text, NumberStyles.Number, culture, out value))
    {
        //Throw away the fractional part
        //return (int)value;

        //or make some rounding??
        return (int)Math.Round(value, MidpointRounding.ToEven);
    }

    //What should happen if the parsing fails??
    //Return some default value
    return int.MinValue;
    //return 0;

    //Or throw an exception?
    //throw new FormatException();
    //In that case, maybe use directly Decimal.Parse and let this
    //function throw the exception with the correct message.
}

are you sure you want it to be an int? 您确定要成为一个int吗?

this number is a double. 这个数字是双倍。

int num = (int)double.Parse(test); //will be 19

double num = double.Parse(test); //wil be 19.95

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM