[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

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

0
kishan66
Asked:
kishan66
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
malikirfan28Commented:
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
 
abhinaypCommented:
protected decimal scientificTOdecimal(string varstr)
    {
        decimal d = 0.0m;
        decimal.TryParse(varstr, out d);
        return d;
    }
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now