Newer
Older
RbFreqStand / RbFreqStandMeasure / tools / UnitConvertHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Casic.Birmm.RbFreqStandMeasure.tools
{
    public class UnitConvertHelper
    {
        public static double convertHZ(string currentFreq)
        {
            string unit= Regex.Replace(currentFreq, @"\d", "");

            double value= Convert.ToDouble(Regex.Replace(currentFreq, @"[^\d.\d]", ""));

            switch (unit)
            {
                case "μHz":
                    return value/1000/1000;
                case "mHz":
                    return value/1000;
                case "Hz":
                    return value;
                case "kHz":
                    return value*1000;
                case "MHz":
                    return value*1000*1000;
                default:
                    return 0;
            }
        }

        public static double convertS(string currentPeriod)
        {
            string unit = Regex.Replace(currentPeriod,@"\d","");

            double value = Convert.ToDouble(Regex.Replace(currentPeriod, @"[^\d.\d]", ""));

            switch (unit)
            {
                case "ns":
                    return value/1000/1000/1000;
                case "μs":
                    return value/1000/1000;
                case "ms":
                    return value/1000;
                case "s":
                    return value;
                default:
                    return 0;
            }
        }

        public static string convertDt(string eData)
        {
            double result;
            if (eData.ToUpper().Contains("E"))
            {
                double b = double.Parse(eData.ToUpper().Split('E')[0].ToString());//整数部分
                double c = double.Parse(eData.ToUpper().Split('E')[1].ToString());//指数部分
                result = b * Math.Pow(10, c);
            }
            else
            {
                result = double.Parse(eData);
            }

            return result.ToString();
        }


        public static string hzConvert(string bfFreq, string freqValue)
        {
            string unit = Regex.Replace(bfFreq, @"\d", "");

            double _freValue = Convert.ToDouble(freqValue);

            switch (unit)
            {
                case "μHz":
                    return _freValue * 1000 * 1000 + "μHz";
                case "mHz":
                    return _freValue * 1000 + "mHz";
                case "Hz":
                    return _freValue + "Hz";
                case "kHz":
                    return _freValue * 1000 + "kHz";
                case "MHz":
                    return _freValue * 1000 * 1000 + "MHz";
                default:
                    return "0";
            }

        }

        public static string sConvert(string period,string periodValue)
        {
            string unit = Regex.Replace(periodValue, @"\d", "");

            double _periodValue = Convert.ToDouble(periodValue);

            switch (unit)
            {
                case "ns":
                    return _periodValue * 1000 * 1000 * 1000 + "ns";
                case "μs":
                    return _periodValue * 1000 * 1000 + "μs";
                case "ms":
                    return _periodValue * 1000 + "ms";
                case "s":
                    return _periodValue + "s";
                default:
                    return "0";
            }

        }


        /*int convertV(string currentVOLTage)
        {

        }
        */

        /**
         *  
         * 50kHz   "+5.00000082583410E+004"   "+1.99999966521428E-005"
         * 20 "+1.99999966774069E-008"
         * 
         * */
    }
}