Client/Desktop/KMBIM3.0/KMBIM3.0_소스/Utils/NumberFormatter.cs

170 lines
3.4 KiB
C#
Raw Permalink Blame History

using KDCS.Utils;
using System;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Forms;
namespace KMBIM.Revit.Tools.Cmd.Hanger
{
public class NumberFormatter
{
public enum Unit
{
Imperial,
Metric
}
private static Hashtable htNumbers;
public const double _eps = 1.0e-9;
private Unit _FormatUnit;
public Unit FormatUnit
{
get
{
return _FormatUnit;
}
set
{
_FormatUnit = value;
}
}
public NumberFormatter(Unit ut)
{
FormatUnit = ut;
if (NumberFormatter.htNumbers == null)
{
NumberFormatter.htNumbers = new Hashtable();
NumberFormatter.htNumbers.Add("'", 1.0);
NumberFormatter.htNumbers.Add("\"", 0.083333333333333329);
NumberFormatter.htNumbers.Add("m", 3.28083989501);
NumberFormatter.htNumbers.Add("dm", 0.328083989501);
NumberFormatter.htNumbers.Add("cm", 0.0328083989501);
NumberFormatter.htNumbers.Add("mm", 0.00328083989501);
}
}
private bool fuzzyEqual(double a, double b)
{
return Math.Abs(a - b) < _eps;
}
public string FormatValue(double dVal)
{
if (FormatUnit == Unit.Imperial)
{
int num = (int)dVal;
double a_ = (dVal - (double)num) * 12.0;
string text = "";
if (num != 0)
{
text = text + num + "' ";
}
if (!fuzzyEqual(a_, 0.0))
{
text = text + a_.ToString("0.##") + "\"";
}
else if (num == 0)
{
text = "0\"";
}
return text;
}
double num2 = (double)NumberFormatter.htNumbers["mm"];
return (dVal / num2).ToString("0.##") + "mm";
}
public bool ParseValue(string str, ref double dVal)
{
try
{
if (string.IsNullOrEmpty(str))
{
return false;
}
dVal = 0.0;
string[] array = str.ToLower().Split(' ');
if (array.Length == 0)
{
return false;
}
double num = 1.0;
if (FormatUnit == Unit.Metric)
{
num = (double)NumberFormatter.htNumbers["mm"];
}
for (int num2 = array.Length - 1; num2 >= 0; num2--)
{
string A_ = null;
string text = a(array[num2], ref A_);
if (!string.IsNullOrEmpty(text))
{
if (!NumberFormatter.htNumbers.ContainsKey(text))
{
return false;
}
num = (double)NumberFormatter.htNumbers[text];
}
else if (FormatUnit == Unit.Imperial)
{
num = ((num2 <= 0) ? 1.0 : ((double)NumberFormatter.htNumbers["\""]));
}
if (!string.IsNullOrEmpty(A_))
{
double result=0d;;
if (!double.TryParse(A_, out result))
{
return false;
}
dVal += result * num;
}
}
return true;
}
catch (Exception)
{
return false;
}
}
public bool ParsePositiveValue(string value, ref double val)
{
if (!ParseValue(value, ref val))
{
MessageBox.Show("Cannot parse value in string {0}", "<22><><EFBFBD><EFBFBD>");
return false;
}
if (val <= 0.0)
{
MessageBox.Show("Please input a positive number.", "<22><><EFBFBD><EFBFBD>");
return false;
}
return true;
}
private string a(string A_0, ref string A_1)
{
string text = "";
A_1 = "";
char[] array = A_0.Trim().ToCharArray();
int i;
for (i = 0; i < array.Length && ((array[i] >= '0' && array[i] <= '9') || array[i] == '.' || array[i] == '-'); i++)
{
A_1 += array[i];
}
for (; i < array.Length; i++)
{
text += array[i];
}
return text.Trim();
}
}
}