Reputation: 2176
I have a Oracle-view which gives me those data:
DATUM STUNDE LAUFZEIT
-------------- ---------- -----------
30.10.14 00:00 11 ,433333333
The column LAUFZEIT
is declared as NUMBER
. Which format to I need to convert the column to get 0,433333333
or rounded to 0,4
?
I already tried some types like Convert.ToSingle(reader.GetValue(2))
but always get a error like
System.OverflowException: Arithmetic operation resulted in an overflow
Thanks!
Upvotes: 0
Views: 1381
Reputation: 186708
You have to mention a currect Culture:
Object source = ",433333333";
// This will fail with exception - Neutral Culture uses decimal point, not comma
//Single single = Convert.ToSingle(source, CultureInfo.InvariantCulture);
// OK: Russian culture (ru-RU) uses decimal comma, not decimal point
Single single = Convert.ToSingle(source, new CultureInfo("ru-RU"));
To represent the value in desired form, use formatting, e.g. for 0,4:
// F1 - one floating point
// "ru-RU" for decimal comma
String result = single.ToString("F1", new CultureInfo("ru-RU"));
Edit: having seen on the Exception stack trace, i.e.
Arithmetic operation resulted in an overflow. at Oracle.DataAccess.Types.DecimalConv.GetDecimal(IntPtr numCtx)
one can conclude that the problem is in the
`Oracle.DataAccess.Types.DecimalConv.GetDecimal`
the origin of the error may be in the fact that Oracle Number(36)
or the the like is bigger that .Net Decimal
. Since you can't change Oracle.DataAccess
library you can convert to String just in the query:
select ...
cast(LAUFZEIT as VarChar2(40)),
...
Upvotes: 2
Reputation: 1321
you can always add a leading zero yourself before parsing. Adding a zero to the start of a number will NEVER change it.
Convert.ToSingle('0' + reader.GetString(2).Replace(',','.'))
should do it.
I advice to use reader.GetString() before parsing.
Also it would be better to do:
Single a ;
if(Single.TryParse('0' + reader.GetString(2).Replace(',','.')), out a))
{
//Success code here
}
else
{
//Code to execute if string was not parsable here
}
In this way you won't get an exception
Upvotes: 1