利用C#实现inch和cm的互换(含源码)

 

 

原理:1inch = 1/12foot = 2.54cm = 25.4mm
说明:inchcm时可精确转换,cminch时约等。
不当之处望指教。
点击下载:inchEXcm--liong.rar
---------
直接贴出源码:

// 利用:1inch = 1/12foot = 2.54cm = 25.4mm 实现。
namespace  inchEXcm
{
    
public partial class frmMain : Form
    
{
        
public frmMain()
        
{
            InitializeComponent();
        }

        
private double f = 0;
        
private double i = 0;
        
private double c = 0;
        
private double r = 0;

        
//字符串strIn若为空返回0;若为实数返回此实数的双精度值;若含字符串返回-1.
        private double GetNum(string strIn)
        
{
            
if (strIn == "")
            
{
                
return 0;
            }

            
else 
            
{
                
try 
                
{
                    
double re = Convert.ToDouble(strIn);
                    
return re;
                }

                
catch
                
{                   
                    
return -1;
                }

            }

        }

        
        
private void btnConvert_Click(object sender, EventArgs e)
        
{            
            
if (radinch.Checked)
            
{
                
//inch to cm
                f = GetNum(txtFoot.Text);
                
if (f == -1)
                
{
                    MessageBox.Show(
"Number only!""Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
return;
                }

                i 
= GetNum(txtInch.Text);
                
if (i == -1)
                
{
                    MessageBox.Show(
"Number only!""Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
return;
                }

                r 
= f * 12 * 2.54 + i * 2.54;
                txtCm.Text 
= r.ToString();
            }

            
if (radcm.Checked)
            
{
                
//cm to inch
                c = GetNum(txtCm.Text);
                
if (c == -1)
                
{
                    MessageBox.Show(
"Number only!""Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
return;
                }

                r 
= c / 2.54;
                txtFoot.Text 
= Convert.ToInt32(r / 12-r%12/12).ToString();//实现强制取整
                txtInch.Text = Convert.ToInt32(r % 12).ToString();               
            }

        }


        
private void btnCopy_Click(object sender, EventArgs e)
        
{
            
string strCB = "";
            
if (radinch.Checked)
            
{
                strCB 
= txtCm.Text + "cm";
            }

            
if (radcm.Checked)
            
{
                strCB 
= txtFoot.Text + "foot " + txtInch.Text + "inch";
            }

            Clipboard.SetText(strCB);
//复制到剪切板
            MessageBox.Show("Message '"+strCB+"' has been copied!"";)"
                MessageBoxButtons.OK, MessageBoxIcon.Information);
        }


        
private void btnExit_Click(object sender, EventArgs e)
        
{
            
this.Close();
        }


        
private void radinch_CheckedChanged(object sender, EventArgs e)
        
{
            txtInch.Text 
= "0";
            txtFoot.Text 
= "0";
            txtCm.Text 
= "0";
            txtCm.Enabled 
= false;
            txtFoot.Enabled 
= true;
            txtInch.Enabled 
= true;
        }


        
private void radcm_CheckedChanged(object sender, EventArgs e)
        
{
            txtInch.Text 
= "0";
            txtFoot.Text 
= "0";
            txtCm.Text 
= "0";
            txtCm.Enabled 
= true;
            txtFoot.Enabled 
= false;
            txtInch.Enabled 
= false;
        }


        
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        
{
            MessageBox.Show(
"-=  inchEXcm v.01  =-/n/nliong/n20080402""inchEXcm v0.1",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }

}

 

转贴地址:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值