[Original] how to resolve an object to int, double, float?

Source: Internet
Author: User
Tags parse string
Write this article Article Because I saw Allen Lee's how to parse a string to an int? [C #]. He is very helpful and admired for his rigorous attitude.

His article only mentions how to parse string into int, so I have learned to sell it now :) write the conversion of three types (INT, double, float. the younger brother is not talented. Please kindly advise me!

Protected   Void Page_load ( Object Sender, eventargs E)
{
Int Init =   414 ; // Try it :)
String Str =   " 100000000000000000000000000000000000000000000 " ;
Response. Write ( " <Li> "   + Getobj2int (STR, init ));
Response. Write ( " <Li> "   + Getobj2double (STR, init ));
Response. Write ( " <Li> "   + Getobj2float (STR, init ));
}

Public   Static   Float Getobj2float ( Object OBJ, Float Initvalue)
{ // Author: Kennytian@msn.com
String S = OBJ. tostring ();
Float F =   Float . Epsilon; // It only initializes a value and has no other meaning.
Bool Issuccessful =   Float . Tryparse (S, Out F );
If (Issuccessful) // Try not to perform some operations in the judgment
{
Return F;
}
Return Initvalue; // If it fails, return
}

Public   Static   Double Getobj2double ( Object OBJ, Double Initvalue)
{ // Author: Kennytian@msn.com
String S = OBJ. tostring ();
Double D =   Double . Negativeinfinity; // The initial value is-infinity, or 0;
Bool Issuccessful =   Double . Tryparse (S, Out D ); // Returns the bool value of whether the conversion is successful.
If (Issuccessful)
{
Return D;
}
Return Initvalue;
}

Public   Static   Int Getobj2int ( Object OBJ, Int Initvalue)
{ // Author: Kennytian@msn.com
String S = OBJ. tostring ();
Int I =   Int . Minvalue; // The initial value is-2147483648.
Bool Issuccessful =   Int . Tryparse (S, Out I );
If (Issuccessful)
{
Return I; // If the conversion is successful, it is the value of S, not-2147483648.
}
Return Initvalue;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.