利用C#实现inch和cm的互换(含源码)
来源:互联网 发布:孔浩java视频 编辑:程序博客网 时间:2024/06/06 09:53
原理:1inch = 1/12foot = 2.54cm = 25.4mm
说明:inch转cm时可精确转换,cm转inch时约等。
不当之处望指教。
点击下载:inchEXcm--liong.rar
---------
直接贴出源码:
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);
}
}
}
转贴地址:
- 利用C#实现inch和cm的互换(含源码)
- cm源码下载和源码结构介绍,recovery的编译。
- C# 获取汉字的拼音首字母和全拼(含源码)
- 字号、pt、px、inch、cm 之间的关系及对照表
- Web C#2.0 DataSet和Reader封装组件实现自动多数据库切换(含组件源码和实例)
- Web C#2.0 DataSet和Reader封装组件实现自动多数据库切换(含组件源码和实例)
- 下载最新的CM源码
- ZeroMQ C# 和D的互换消息
- android定位之GPS,WIFI和GPRS(NETWORK)方式的定位(含实现源码)
- 下载和编译cm源码及制作cm刷机包
- android 源码和CM源码下载地址
- 利用AsyncUdpSocket实现局域网下的IM(含Demo)
- 利用AsyncUdpSocket实现局域网下的IM(含Demo)
- C#判断上班时间和下班时间(含周末)的方法实现
- 编译 cyanogenmod (cm) android 源码遇到的坑(自用)
- CM(CDH)的安装过程破解(含卸载)--自己可以随意怎么安装都行了
- STL 简单 iterator 的实现(含源码)
- 利用异或运算实现两个变量的互换(学习)
- 给大家漏一手本人亲自精心撰写的通用ajax框架,完全兼容 IE FireFox各个版本!(附完整源码及完整范例)
- ASP.NET中常用功能代码总结(4)——保存图片到XML文件
- linux动态链接库的创建与使用
- UltraWinGrid自适应列宽/行高
- 给大家漏一手本人亲自精心撰写的通用ajax框架,完全兼容 IE FireFox各个版本!(附完整源码及完整范例)
- 利用C#实现inch和cm的互换(含源码)
- 13个不可不知的ASP.NET MVC扩展点
- JS document.createElement 动态添加表
- 新征程
- ASP.NET中常用功能代码总结(5)——文件操作篇
- 去云南拍的一些风光照片
- 写了一个能自定义并自动按照规则生成单据号码的程序及相关存储过程和表,及示范,附全部源代码!
- ubuntu 9.10 字体美化攻略
- mysqladmin的用法