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;
}