开发者

there is a strange behaviour double parse string input?

开发者 https://www.devze.com 2023-02-02 10:21 出处:网络
i try to read some excel file and converting some string column to save in db. BUT i face to face some double parse error: double result = double.Parse( 1,15);

i try to read some excel file and converting some string column to save in db. BUT i face to face some double parse error: double result = double.Parse( 1,15); result: 1.149999999999.... i dont want to see this. i want double result = double.Parse( 1,15); result = 1.15


 static v开发者_如何学编程oid Main(string[] args)
        {
            NumberStyles styles;
            IFormatProvider provider;

            styles = NumberStyles.Float;
            provider = CultureInfo.CreateSpecificCulture("tr-TR");

            string test = "1,15";
            double result = double.Parse(test, styles, CultureInfo.CreateSpecificCulture("tr-TR"));
            Console.WriteLine(result.ToString());
        }
    }


Use the decimal data type for DB and Application if you need the high precision data types.


You parse as a float, but convert to double -- float isn't accurate, hence why you're seeing the error.

Your code should be:

double result = double.Parse(...);

and not:

double result = float.Parse(...);

Edit:

Side note: When you convert something to a string, use value.ToString("R") if you want a round-trip to occur; that would represent the exact value as a string, not a truncated value.


looks like error in the code: double result = float.Parse(); double.parse() should work fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消