public static LDim Parse(SqlString s) { if (s.IsNull) { return(Null); } // regular expression to test, extract string fp = @"-?([0-9]+(\.[0-9]*)?|\.[0-9]+)"; Regex vu = new Regex(@"(?<v>" + fp + @") (?<u>in|ft|yd)"); if (!vu.IsMatch(s.Value)) { throw new Exception("Bad format", null); } Match m = vu.Match(s.Value); LDim d = new LDim(); d.units = m.Result("${u}"); d.value = decimal.Parse(m.Result("${v}")); if (!CheckRangeAndAccurcy(d)) { throw new Exception("Out of range or accuracy", null); } return(d); }
public static SqlString Reverse(LDim dim) { if (dim.IsNull) { return(SqlString.Null); } string sdim = dim.ToString(); char[] chs = sdim.ToCharArray(); Array.Reverse(chs); return(new string(chs)); }
public LDim GetScaledCNR(SqlDecimal scale) { LDim ldim = new LDim(); if (!IsNull) { ldim.value = this.value; ldim.units = this.units; ldim.value *= scale.Value; if (!CheckRangeAndAccurcy(ldim)) { ldim = this; } } return(ldim); }
static bool CheckRangeAndAccurcy(LDim d) { decimal value = d.value; // check overall range if (d.units == "in") { if ((value > (52800M * 12M)) || (value < -(52800M * 12M))) { return(false); } } if (d.units == "ft") { if ((value > 52800M) || (value < -52800M)) { return(false); } } if (d.units == "yd") { if ((value > (52800M / 3M)) || (value < -(52800M) / 3M)) { return(false); } } // check accuracy to 0.001 inches if (d.units == "ft") { value *= 12M; } if (d.units == "yd") { value *= 36M; } decimal norm = value * 1000M; norm = decimal.Round(norm); if ((norm - (value * 1000M)) != 0M) { return(false); } return(true); }
static public LDim operator +(LDim d1, LDim d2) { if (d1.IsNull || d2.IsNull) { return(Null); } if (d1.units == d2.units) { LDim dim = new LDim(); dim.units = d1.units; dim.value = d1.value + d2.value; return(dim); } LDim dim1 = new LDim(); dim1.units = "in"; dim1.value = d1.Inches.Value + d2.Inches.Value; return(dim1); }
void IXmlSerializable.ReadXml(System.Xml.XmlReader reader) { XPathDocument xdoc = new XPathDocument(reader); XPathNavigator nav = xdoc.CreateNavigator(); XmlNamespaceManager nm = new XmlNamespaceManager(nav.NameTable); nm.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); if ((bool)(nav.Evaluate("boolean(//@xsi:nil = 'true')", nm))) { value = 0; units = null; } else { LDim dim = Parse((string)(nav.Evaluate( "concat(//value, ' ', //units)"))); units = dim.units; value = dim.value; } }