Data type conversions in C #

Source: Internet
Author: User
Tags cos sin split tostring
Data | data type | conversion

Programming in C #, the parameter type is not as tolerant as VB, inevitably to the various types of mutual conversion, such as the data entered in the text control in the calculation must be converted to the corresponding numeric types to be calculated. Usually we can do this in three ways:

1. Force type Conversion

private void TST ()

{

String t_string= "1.4532";

Double T_rtn;

T_rtn=math.cos (double) t_string);

...

}

2, Parse method

Classes with double, string, and so on have a method named Parse, which we can use to convert other types of variables to this type:

private void Button1_Click (object sender, System.EventArgs e)
{
Double sp_x,sp_y,dis,azm,p_x,p_y;
String alpha;string[] aztmp;
Double Ad,am,as; Degrees, minutes, seconds

Alpha=az. Text;
Aztmp=alpha.split ('. ');
Ad=convert.todouble (Aztmp[0]);
Am=convert.todouble (Aztmp[1]. Substring (0,2));
As=convert.todouble (Aztmp[1]. Substring (2,2));

Dbg.text =convert.tostring (Ad) + "" + convert.tostring (Am) + "" +convert.tostring (AS);

Sp_x=double. Parse (SX. Text);


Sp_y=double. Parse (sy. Text);


Dis=double. Parse (D.text);


azm= (ad+am/60.0+as/3600) * MATH.PI/180;
p_x= (Double) System.Math.Cos (AZM) * dis+sp_x;
P_y= (Double) System.Math.Sin (AZM) * DIS+SP_Y;
Px. Text=p_x.tostring ();
Py. Text=p_y.tostring ();
Point3 p3=new Point3 ();
P3.x=1.256;p3.y =2.356;p3.h=100;
Dbg.text=p3. ToString ();

}

3. Convert type

private void Button1_Click (object sender, System.EventArgs e)
{
Double sp_x,sp_y,dis,azm,p_x,p_y;
String alpha;string[] aztmp;
Double Ad,am,as; Degrees, minutes, seconds

Alpha=az. Text;
Aztmp=alpha.split ('. ');
Ad=convert.todouble (Aztmp[0]);


Am=convert.todouble (Aztmp[1]. Substring (0,2));


As=convert.todouble (Aztmp[1]. Substring (2,2));

Dbg.text =convert.tostring (Ad) + "" + convert.tostring (Am) + "" +convert.tostring (AS);

Sp_x=double. Parse (SX. Text);

Sp_y=double. Parse (sy. Text);


Dis=double. Parse (D.text);


azm= (ad+am/60.0+as/3600) * MATH.PI/180;
p_x= (Double) System.Math.Cos (AZM) * dis+sp_x;
P_y= (Double) System.Math.Sin (AZM) * DIS+SP_Y;
Px. Text=p_x.tostring ();
Py. Text=p_y.tostring ();
Point3 p3=new Point3 ();
P3.x=1.256;p3.y =2.356;p3.h=100;
Dbg.text=p3. ToString ();

}



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.