Example #1
0
 public void RunBeforeEachTest()
 {
     Precision p = new Precision();
     this.Name = p.Name;
     this.PrecisionType = p.PrecisionType;
     this.FaceValue = p.FaceValue;
     this.NumberOfDigits = p.NumberOfDigits;
 }
        public static string CalculateRoundedScaledNumber( decimal number, decimal rounding, Precision precisionWrapper )
        {
            decimal tmpRounding = rounding;
            int scaleFactor = 0;
            while( tmpRounding >= 10 )
            {
                scaleFactor++;
                tmpRounding /= 10;
            }

            int precision = 0;
            if( precisionWrapper != null )
                precision = precisionWrapper.NumberOfDigits * -1;

            if( scaleFactor >= precision )
            {
                decimal scaledNumber = number / rounding;

                int decimalPlaces = scaleFactor - precision;
                decimal roundedNumber = Round( scaledNumber, decimalPlaces );

                string decimalFormat = new String( '0', decimalPlaces );
                if( decimalFormat.Length > 0 )
                    decimalFormat = "." + decimalFormat;

                //in this scenario, the scaling is GREATER than the rounding
                //therefore we don't have any decimals
                string numberFormat = string.Format( "###############0{0};-###############0{0};0{0}", decimalFormat );
                string formattedNumber = roundedNumber.ToString( numberFormat );
                return formattedNumber;
            }
            else
            {
                decimal scaledNumber = number / rounding;

                //this must be negative or 0
                //ex:
                //Math.Round( 123456, -3 ) == 123000
                int roundoffPlaces = scaleFactor - precision;
                Debug.Assert( roundoffPlaces <= 0 );

                decimal roundedNumber = Round( scaledNumber, roundoffPlaces );

                string decimalFormat = new String( '0', 0 );
                if( decimalFormat.Length > 0 )
                    decimalFormat = "." + decimalFormat;

                //in this scenario, the scaling is LESS than the rounding
                //therefore we don't have any decimals
                string formattedNumber = roundedNumber.ToString( "###############0;-###############0;0" );
                return formattedNumber;
            }
        }
Example #3
0
 private Precision CreatePrecision( Precision.PrecisionTypeCode typeCode, int decimals )
 {
     return new Precision( typeCode, decimals );
 }
Example #4
0
 private Precision CreatePrecision( Precision.PrecisionTypeCode typeCode )
 {
     return new Precision( typeCode );
 }
Example #5
0
 public void CopyPrecision( InstanceReportRow arg )
 {
     if( arg.MyPrecision != null )
     {
         this.MyPrecision = new Precision( arg.MyPrecision.PrecisionType, arg.MyPrecision.NumberOfDigits );
     }
 }
Example #6
0
        public void Test_Append_DecimalINF()
        {
            XmlDocument doc = new XmlDocument();

            XmlElement elem = doc.CreateElement( "test" );
            doc.AppendChild( elem );

            Precision pp = new Precision( Precision.PrecisionTypeCode.Decimals );

            elem.Attributes.Append( pp.CreateAttribute( doc ) );

            string expectedXML =
                @"<?xml version=""1.0"" encoding=""utf-16""?>
            <test decimals=""INF"" />";

            System.IO.StringWriter writer = new System.IO.StringWriter();

            doc.Save( writer );
            Assert.AreEqual( expectedXML, writer.ToString() );
        }
Example #7
0
        public void TestGetTreeNodeContainerAt()
        {
            Precision obj = new Precision();
            Precision obj2 = new Precision();

            AddTreeNode( obj );
            Assert.AreEqual(1, TreeNodes.Count, "Incorrect number of nodes");
            AddTreeNode( obj2 );
            Assert.AreEqual(2, TreeNodes.Count, "Incorrect number of nodes");

            Precision badObj = GetTreeNodeContainerAt(5) as Precision;
            Assert.IsNull(badObj, "Oject should not have been found");

            Precision tmp1 = GetTreeNodeContainerAt(0) as Precision;
            Assert.AreSame(obj, tmp1, "Tree node at index is not correct");
            Precision tmp2 = GetTreeNodeContainerAt(1) as Precision;
            Assert.AreSame(obj2, tmp2, "Tree node at index is not correct");
        }
Example #8
0
        public void Test_FromXml_Append_Decimals_M7()
        {
            XmlDocument doc = new XmlDocument();

            XmlElement elem = doc.CreateElement( "test" );
            doc.AppendChild( elem );

            Precision pp = new Precision( Precision.PrecisionTypeCode.Decimals, -7 );

            XmlAttribute attr = pp.CreateAttribute( doc );

            Precision p = null;

            ArrayList errors = new ArrayList();

            Assert.IsTrue( Precision.TryCreateFromXml( attr, out p, ref errors ), "precision not created" );

            Assert.IsTrue( p.ValueEquals( pp ), "xml precision different from the real deal" );
        }
Example #9
0
 public void ApplyRounding( Precision precision, decimal divisor )
 {
     if( Math.Abs( this.NumericAmount ) >= 100 )
     {
         this.RoundedNumericAmount =
             InstanceReport.CalculateRoundedScaledNumber( this.NumericAmount, divisor, precision );
     }
     else
     {
         this.RoundedNumericAmount = this.NumericAmount.ToString();
     }
 }
Example #10
0
 /// <summary>
 /// Sets the precision reference and text properties of this <see cref="MarkupProperty"/>.
 /// </summary>
 /// <param name="pp">The precision type of this <see cref="Precision"/> will be 
 /// included in precision text property.</param>
 public void SetPrecision(Precision pp)
 {
     precisionDef = pp;
     precisionText = (pp == null) ? string.Empty : "precision: " + pp.PrecisionType.ToString();
 }
Example #11
0
        public static RoundingLevel GetRoundingLevelFromPrecision(Precision prec)
        {
            if( prec == null )
                return RoundingLevel.UnKnown;

            if( prec.PrecisionType != Precision.PrecisionTypeCode.Decimals )
                return RoundingLevel.UnKnown;

            if( prec.NumberOfDigits > 0 )
                return RoundingLevel.NoRounding;

            int posNumDigits = prec.NumberOfDigits * -1;
            if( !Enum.IsDefined( typeof( RoundingLevel ), posNumDigits ) )
                return RoundingLevel.UnKnown;

            RoundingLevel rounding = (RoundingLevel)posNumDigits;

            //Ensure that `tens` and `hundreds` are not rounded
            if( rounding == RoundingLevel.Hundreds || rounding == RoundingLevel.Tens )
                return RoundingLevel.NoRounding;
            else
                return rounding;
        }
Example #12
0
        /// <summary>
        /// This method tries to create a Precision object from xml attribute.
        /// </summary>
        /// <param name="attr">XML attribute.</param>
        /// <param name="p">The precision object to be created.</param>
        /// <param name="errors">If the precison can't be created, returns the exceptions.</param>
        /// <returns>Returns true, if the precision object can be created.</returns>
        public static bool TryCreateFromXml( XmlAttribute attr, out Precision p, ref ArrayList errors )
        {
            p = new Precision();
            if ( errors == null )
            {
                errors = new ArrayList();
            }

            if ( attr.LocalName == DECIMALS )
            {
                p.PrecisionType = PrecisionTypeCode.Decimals;
            }
            else
            {
                p.PrecisionType = PrecisionTypeCode.Precision;
            }

            if ( attr.Value.CompareTo( INF ) == 0 )
            {
                p.FaceValue = true;
            }
            else
            {
                try
                {
                    p.NumberOfDigits = int.Parse( attr.Value );
                }
                catch ( FormatException )
                {
                    return false;
                }
            }

            return true;
        }
Example #13
0
        /// <summary>
        /// Appends unit nodes to a parameter supplied root element if they do not exist in <see cref="xDoc"/>.  
        /// Sets the "unitRef" attribute to reflect the unit id 
        /// of a parameter-supplied <see cref="UnitProperty"/>.  
        /// </summary>
        /// <param name="root">The document-level root element to which elements will be appended.</param>
        /// <param name="elem">The element whose attributes are to be set.</param>
        /// <param name="errors">A collection of <see cref="String"/> objects to which method 
        /// will append any errors.</param>
        /// <param name="presProp">A <see cref="Precision"/> from which precision attributes will be created.</param>
        /// <param name="unit">A <see cref="UnitProperty"/> from which unit elements will be created.</param>
        protected void AppendUnitInfo(XmlElement root, XmlElement elem, UnitProperty unit, Precision presProp, ArrayList errors)
        {
            elem.SetAttribute( UNIT_REF, unit.UnitID );

            if ( presProp != null )
            {
                elem.Attributes.Append( presProp.CreateAttribute( xDoc ) );
            }

            string mergeKey = mergeDocs ? @"//link2:" : @"//";
            if ( this.xDoc.SelectSingleNode( string.Format( mergeKey + "unit[@id=\"{0}\"]", unit.UnitID ), theManager ) == null )
            {
                XmlElement unitNode = null;
                if ( mergeDocs )
                {
                    units.Add( unit );
                    if ( unit.CreateElementWithNamespaces( xDoc, root, errors, out unitNode, this.writeComments ) )
                    {
                        root.InsertAfter( unitNode, unitComment );
                    }
                }
                else
                {
                    if ( unit.CreateElement( xDoc, root, errors, out unitNode, this.writeComments ) )
                    {
                        root.InsertAfter( unitNode, unitComment );
                    }
                }
            }
        }