C#十进制.解析行为 [英] C# decimal.Parse behaviour

查看:97
本文介绍了C#十进制.解析行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短问题:

为什么这些" ..... "对于解析.NET(C#)中的小数有效?

Why are these '.....' valid for parsing a decimal in .NET (C#):

decimal res = decimal.Parse("8......15");  // returns 815
decimal res = decimal.Parse("8...15");     // returns 815
decimal res = decimal.Parse("8..15");      // returns 815

这是什么原因?

推荐答案

对我来说失败了.您是否偶然在."的文化中是千位分隔符,,"是小数点? Decimal.Parse(和类似的调用)默认情况下使用线程的当前区域性.这是好事还是有待商but,但与实际行为无关:)

It fails for me. Are you by any chance in a culture where "." is the thousands separator and "," is the decimal point? Decimal.Parse (and similar calls) use the thread's current culture by default. Whether that is a good thing or not is debatable, but irrelevant to actual behaviour :)

尝试在decimal.Parse调用中指定CultureInfo.InvariantCulture:

decimal res = decimal.Parse("8......15", CultureInfo.InvariantCulture);

我相信这会按照您的预期进行.

I believe that will behave as you expected.

这篇关于C#十进制.解析行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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