package com.ng.ngtools.modules;

import com.ng.ngtools.tangible.FloatingPointToInteger;

public class UnitConvert
 {
	  public final double Converter(String UnitName, double oldValue, int oldunit, int newunit, int Xsdws)
	  {
		double returnValue = 0;
		//On Error Resume Next VBConversions Warning: On Error Resume Next not supported in C#
		//Dim strUnit() As String
		double[] dataUnit = new double[] {0};

		//ReDim strUnit(UnitNum)
		//RereDim dataUnit(UnitNum)
		switch (UnitName)
		{
		  case "tj":

			dataUnit = new double[8];


			//strUnit(0) = "立方米(m3)"
			//strUnit(1) = "升(L, dm3)"
			//strUnit(2) = "立方厘米(cm3, ml, c.c)"
			//strUnit(3) = "立方英尺(ft3)"
			//strUnit(4) = "立方英寸(in3)"
			//strUnit(5) = "英加仑(UKgal)"
			//strUnit(6) = "美加仑(U.Sgal)"
			//strUnit(7) = "美油桶(USbbl)"

			dataUnit[0] = 1;
			dataUnit[1] = 1000;
			dataUnit[2] = 1000000;
			dataUnit[3] = 35.3147;
			dataUnit[4] = 61023.7;
			dataUnit[5] = 219.969;
			dataUnit[6] = 264.172;
			dataUnit[7] = 6.28994;
			break;

		  case "zl":

			//strUnit(0) = "千克(公斤)(kg)"
			//strUnit(1) = "克(g)"
			//strUnit(2) = "毫克(mg)"
			//strUnit(3) = "吨(t)"
			//strUnit(4) = "英吨(长吨)(UKton)"
			//strUnit(5) = "美吨(短吨)(U.Ston)"
			//strUnit(6) = "磅(lb)"
			//strUnit(7) = "盎司(oz)"
			dataUnit = new double[8]; //As Double


			dataUnit[0] = 1;
			dataUnit[1] = 1000;
			dataUnit[2] = 1000000;
			dataUnit[3] = 0.001;
			dataUnit[4] = 0.000984207;
			dataUnit[5] = 0.00110231;
			dataUnit[6] = 2.20462;
			dataUnit[7] = 35.274;
			break;
		  case "rl":
			//strUnit(0) = "焦耳(J)"
			//strUnit(1) = "马力小时(Hp·h)"
			//strUnit(2) = "公斤力·米(kgf·m)"
			//strUnit(3) = "升·大气压(L·atm)"
			//strUnit(4) = "尔格(erg)"
			//strUnit(5) = "千卡(kacl)"
			//strUnit(6) = "千瓦小时(kW·h)"
			//strUnit(7) = "英马力小时(UKHp·h)"
			//strUnit(8) = "英尺·磅力(ft·lbf)"
			//strUnit(9) = "英热单位(BTU)"
			dataUnit = new double[10]; // As Double


			dataUnit[0] = 1;
			dataUnit[1] = 0.000000377672;
			dataUnit[2] = 0.101972;
			dataUnit[3] = 0.00986923;
			dataUnit[4] = 107;
			dataUnit[5] = 0.000238846;
			dataUnit[6] = 0.000000277778;
			dataUnit[7] = 0.000000372506;
			dataUnit[8] = 0.737562;
			dataUnit[9] = 0.000947813;
			break;


		  case "nlll":
			//strUnit(0) = "兆焦/秒(MJ/s)"
			//strUnit(1) = "兆焦/小时(MJ/h)"
			//strUnit(2) = "兆焦/天(MJ/d)"
			//strUnit(3) = "千卡/秒(kcal/s)"
			//strUnit(4) = "千卡/小时(kcal/h)"
			//strUnit(5) = "英热单位/秒(BTU/s)"
			//strUnit(6) = "英热单位/小时(BTU/h)"

			dataUnit = new double[7]; //As Double

			dataUnit[0] = 1;
			dataUnit[1] = 3600;
			dataUnit[2] = 86400;
			dataUnit[3] = 238.846;
			dataUnit[4] = 859845.6;
			dataUnit[5] = 947.813;
			dataUnit[6] = 3412126.8;
			break;

		  case "zlll":

			//strUnit(0) = "千克(公斤)/秒(kg/s)"
			//strUnit(1) = "千克(公斤)/分(kg/Min)"
			//strUnit(2) = "千克(公斤)/时(kg/h)"
			//strUnit(3) = "磅/秒(lb/s)"
			//strUnit(4) = "磅/分(lb/Min)"
			//strUnit(5) = "磅/时(lb/h)"
			//strUnit(6) = "吨/时(t/h)"
			//strUnit(7) = "英吨(长吨)/时(UKton/h)"
			//strUnit(8) = "美吨(短吨)/小时(U.Ston/h)"

			dataUnit = new double[9]; //As Double

			dataUnit[0] = 1;
			dataUnit[1] = 60;
			dataUnit[2] = 3600;
			dataUnit[3] = 2.20462;
			dataUnit[4] = 132.2772;
			dataUnit[5] = 7936.632;
			dataUnit[6] = 3.6;
			dataUnit[7] = 3.543145;
			dataUnit[8] = 3.968316;
			break;
		  case "tjll":
			//strUnit(0) = "立方米/秒(m3/s)"
			//strUnit(1) = "万立方米/天(m3/d)"
			//strUnit(1) = "立方米/时(m3/h)"
			//strUnit(2) = "立方米/分(m3/Min)"
			//strUnit(3) = "升/时(L/h)"
			//strUnit(4) = "升/分(L/Min)"
			//strUnit(5) = "升/秒(L/s)"
			//strUnit(6) = "立方英尺/时(ft3/h)"
			//strUnit(7) = "立方英尺/分(ft3/Min)"
			//strUnit(8) = "立方英尺/秒(ft3/s)"
			//strUnit(9) = "立方英尺/秒(ft3/s)"
			//strUnit(10) = "立方英尺/天(ft3/d)"

			//strUnit(11) = "英加仑/秒(UKgal/s)"
			//strUnit(12) = "美加仑/秒(U.Sgal/s)"
			//strUnit(13) = "美油桶/秒(USbbl/s)"


			dataUnit = new double[13]; //As Double

			dataUnit[0] = 1;
			dataUnit[1] = 8.64;
			dataUnit[2] = 3600;
			dataUnit[3] = 60;
			dataUnit[4] = 3600000;
			dataUnit[5] = 60000;
			dataUnit[6] = 1000;
			dataUnit[7] = 127132.92;
			dataUnit[8] = 2118.882;
			dataUnit[8] = 0.0245240972222222;
			dataUnit[9] = 35.3147;
			dataUnit[10] = 219.969;
			dataUnit[11] = 264.172;
			dataUnit[12] = 6.28994;
			break;


		  case "yl":
			//strUnit(0) = "帕(Pa)"
			//strUnit(1) = "千帕(kPa)"
			//strUnit(2) = "兆帕(Mpa)"
			//strUnit(3) = "标准大气压(atm)"
			//strUnit(4) = "毫巴(mbar)"
			//strUnit(5) = "巴(bar)"
			//strUnit(6) = "千克力/平方米(kgf/m2)"
			//strUnit(7) = "千克力/平方厘米(kgf/cm2)"
			//strUnit(8) = "毫米汞柱(mmHg)"
			//strUnit(9) = "毫米水柱4℃(mmH2O)"
			//strUnit(10) = "磅/平方英寸(psi)"

			dataUnit = new double[11]; //As Double

			dataUnit[0] = 1;
			dataUnit[1] = 0.001;
			dataUnit[2] = 0.000001;
			dataUnit[3] = 0.00000986923266716013;
			dataUnit[4] = 0.01;
			dataUnit[5] = 0.00001;
			dataUnit[6] = 0.101971621;
			dataUnit[7] = 0.0000101972;
			dataUnit[8] = 0.007500638;
			dataUnit[9] = 0.101972;
			dataUnit[10] = 0.000145038;
			break;

		  case "wd":
			switch (newunit)
			{
			  case 0:
				switch (oldunit)
				{
				  case 0:
					returnValue = oldValue;
					break;
				  case 1: //K->℃
					returnValue = oldValue - 273.15;
					break;
				  case 2: //F->℃
					returnValue = (oldValue - 32) / 1.8;
					break;
				  case 3: //R->℃
					returnValue = oldValue / 1.8 - 273.15;
					break;
				}
				break;
			  case 1:
				switch (oldunit)
				{
				  case 0: //℃->K
					returnValue = oldValue + 273.15;
					break;
				  case 1:
					returnValue = oldValue;
					break;
				  case 2: //F->K
					returnValue = (oldValue - 32) / 1.8 + 273.15;
					break;
				  case 3: //R->K
					returnValue = oldValue / 1.8;
					break;
				}
				break;
			  case 2:
				switch (oldunit)
				{
				  case 0: //C->F
					returnValue = oldValue * 1.8 + 32;
					break;
				  case 1: //k->f
					returnValue = (oldValue - 273.15) * 1.8 + 32;
					break;
				  case 2:
					returnValue = oldValue;
					break;
				  case 3: //R->F
					returnValue = oldValue - 273.15 * 1.8 + 32;
					break;

				}
				break;
			  case 3:
				switch (oldunit)
				{
				  case 0: //C->R
					returnValue = (oldValue + 273.15) * 1.8;
					break;
				  case 1: //K->R
					returnValue = oldValue * 1.8;
					break;
				  case 2: //F->R
					returnValue = (oldValue - 32) + 273.15 * 1.8;
					break;
				  case 3:
					returnValue = oldValue;
					break;
				}
				break;
			  default:
				break;
			}
			return returnValue;
		  case "cd":
			//米(m)
			//分米(dm)
			//厘米(cm)
			//毫米(mm)
			//英尺(ft)
			//英寸(in)
			//英里(mile)
			//英寻(fm)
			//海里(nmile)
			//埃(a)
			//码(yd)
			//密尔(mil)
			//杆<(rad)

			dataUnit = new double[14]; //As Double


			dataUnit[0] = 1;
			dataUnit[1] = 10;
			dataUnit[2] = 100;
			dataUnit[3] = 1000;
			dataUnit[4] = 3.28038;
			dataUnit[5] = 39.3700787401575;
			dataUnit[6] = 0.001;
			dataUnit[7] = 0.000621504039776259;
			dataUnit[8] = 0.546746856205577;
			dataUnit[9] = 0.000539956803455;
			dataUnit[10] = 10000000000.0D;
			dataUnit[11] = 1.093613;
			dataUnit[12] = 39370.0787401575;
			dataUnit[13] = 0.198838781515947;
			break;

		  case "mj":


			//平方米m2
			//平方分米dm2
			//平方厘米cm2
			//平方毫米mm2
			//平方英尺ft2
			//平方英寸in2
			//平方公里km2
			//公顷ha
			//公亩are
			//英亩acre
			//平方英里sq -mile
			//平方码yd2

			dataUnit = new double[12]; //As Double


			dataUnit[0] = 1;
			dataUnit[1] = 100;
			dataUnit[2] = 10000;
			dataUnit[3] = 1000000;
			dataUnit[4] = 10.7608929444;
			dataUnit[5] = 1550.0031;
			dataUnit[6] = 0.000001;
			dataUnit[7] = 0.0001;
			dataUnit[8] = 0.01;
			dataUnit[9] = 0.0002471;
			dataUnit[10] = 0.000000386102158;
			dataUnit[11] = 1.19599;
			break;
		  //小时
		  //分钟
		  //天
		  //秒

		  case "sj":
			dataUnit = new double[4]; //As Double

			dataUnit[0] = 1;
			dataUnit[1] = 60;
			dataUnit[2] = 0.0416666666666667;
			dataUnit[3] = 3600;
			break;



		  case "sd":
			break;
		  //米每秒
		  //厘米每秒
		  //
		}
		if (newunit == oldunit)
		{
		  return oldValue;

		}
		return SsWr(oldValue * dataUnit[newunit] / dataUnit[oldunit], Xsdws);

	  }

	  private double SsWr(double value, int weishu)
	  {
		long tempValue = 0;
		double SorR = 0;
		try
		{
		  tempValue = (long)(value * Math.pow(10, weishu));

		  SorR = FloatingPointToInteger.ToInt32((value * Math.pow(10, weishu) - tempValue) * 10);
		  if (SorR >= 5)
		  {
			tempValue++;
		  }
		  return tempValue / Math.pow(10, weishu);
		}
		catch (Exception e)
		{

		  return value;

		}
	  }







 }