Oracle 数字转 C# 十进制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1906361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Oracle number to C# decimal
提问by robert.oh.
I know there are several threads and posts regarding this issue in the internet and I've read them (not every article, I have to admit) but none of them did fully satisfy me.
我知道互联网上有几个关于这个问题的主题和帖子,我已经阅读了它们(不是每篇文章,我必须承认),但没有一个让我完全满意。
My situation:
I'm using ODP.net (dll version 2.111.6.0) to access the Oracle DB (version 10 + 11) and a DataReader to retrieve the data (.NET 3.5, C#).
我的情况:
我使用 ODP.net(dll 版本 2.111.6.0)访问 Oracle DB(版本 10 + 11)和 DataReader 来检索数据(.NET 3.5,C#)。
Using this code results in a 'System.OverflowException (Arithmetic operation resulted in an overflow.)'
使用此代码会导致“ System.OverflowException(算术运算导致溢出。)”
decimal.TryParse(oraReader.GetOracleDecimal(0).Value.ToString(),
NumberStyles.Any, null, out parsedOraDecimal)
and this one results in a value of '3,000000000000000000000000000000000000000000000000000000000E-126'
这个结果是' 3,000000000000000000000000000000000000000000000000000000000E-126'
decimal.TryParse(oraReader.GetOracleValue(0).ToString(),
NumberStyles.Any, null, out parsedOraDecimal)
Now I have to find some way to retrieve and evaluate this value properly - the DB is also used from other apps which are out of my control so changes there are not possible.
现在我必须找到某种方法来正确地检索和评估这个值 - 数据库也从我无法控制的其他应用程序中使用,因此无法进行更改。
Converting the types in my C# code from 'decimal' to 'double' is also not really an option.
将我的 C# 代码中的类型从“decimal”转换为“double”也不是一个真正的选择。
Any ideas?
有任何想法吗?
采纳答案by Dave Glassborow
I just had a similar issue, and tried the approach of changing the OracleDataAdapter to return Oracle specific types ( data_adapter.ReturnProviderSpecificTypes = true; ), but this is just a PITA, you end up casting OracleStrings back to strings, etc.
我刚刚遇到了类似的问题,并尝试了更改 OracleDataAdapter 以返回 Oracle 特定类型( data_adapter.ReturnProviderSpecificTypes = true; )的方法,但这只是一个 PITA,您最终将 OracleStrings 转换回字符串等。
In the end I solved it by doing the precision rounding in the SQL statement using Oracle's round function:
最后我通过使用 Oracle 的 round 函数在 SQL 语句中进行精度舍入来解决它:
SELECT round( myfield, 18 ) FROM mytable
Dotnet will then happily convert the figure to a decimal.
然后,Dotnet 会很高兴地将数字转换为小数。
回答by Erich Kitzmueller
OracleDecimal has a larger precision than decimal. For that reason, you have to reduce the precision first. Forget all the parsing, use implicit conversion. Try something along the lines of (untested):
OracleDecimal 的精度高于十进制。因此,您必须首先降低精度。忘记所有的解析,使用隐式转换。尝试类似(未经测试)的方法:
decimal d = (decimal)(OracleDecimal.SetPrecision(oraReader.GetOracleDecimal(0), 28));