Check datatype in C#.net

Hi,
i need to call a user-defined conversion function "ScientificToDecimal" when ever value retrieved is not float

  some times values is like   >> 2.125900000000000e+004        OR        >> 2.12
so i want to convert  from Scientific to Decimal if the value is not float datatype.
Note:- 'ScientificToDecimal'  function works fine though

Is there a way ?

Environment:-  C#net,  ASP.Net 2.0


/// sample code ////
 if ((float.TryParse(drowpnl["start_qty"].ToString(), out flt)))
{
 decimal dc_start_qty =  scientificTOdecimal(drowpnl["start_qty"].ToString());
 drowpnl["start_qty"] = string.Format("{0:N2}", dc_start_qty);
}

//// ScientificTodecimal /////
 protected decimal scientificTOdecimal(string varstr)
 {
   return (decimal.Parse(varstr,    System.Globalization.NumberStyles.Float));
 }

Open in new window

kishan66Asked:
Who is Participating?
 
malikirfan28Connect With a Mentor Commented:
Hello, if x has any decimal value or a scientific value which can be converted into decimal then above given lines will work for such value.
if x has such values which can't be converted to decimal then above lines at Decimal.Parse will throw an exception which you can handle.

Kindly explain what else you need with example.
0
 
malikirfan28Commented:
try the following code
string x = "2.125900000000000e+004";
            double d = Double.Parse(x, System.Globalization.NumberStyles.Float)

Open in new window

0
 
kishan66Author Commented:
Hi mali kirfan28,

as i mentioned, String x is not always  in this format ( Ex:-"2.125900000000000e+004";)

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Todd GerbertIT ConsultantCommented:
I'm not sure what you're asking...do you mean if the string is "Hello World" you don't want to try and convert it to a decimal?
0
 
kishan66Author Commented:
Hello tgerbert,

i am trying read values from xml file ... which are in Scientific format... and some in decimal

so i want to convert those which are in scientific format Ex:-"2.125900000000000e+004";

do i make any sense........
i 'm sorry if my basic approach is wrong... would appreciate if corrected



0
 
Todd GerbertIT ConsultantCommented:
So if it's NOT in scientific notation you don't want to convert it, even if it's a valid  decimal value?

Give me an example of what should be converted, and what shouldn't, and why.
0
 
abhinaypConnect With a Mentor Commented:
protected decimal scientificTOdecimal(string varstr)
    {
        decimal d = 0.0m;
        decimal.TryParse(varstr, out d);
        return d;
    }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.