SharpVectors.Dom.Css.CssPrimitiveValue.GetFloatValue C# (CSharp) Method

GetFloatValue() public method

This method is used to get a float value in a specified unit. If this CSS value doesn't contain a float value or can't be converted into the specified unit, a DOMException is raised
INVALID_ACCESS_ERR: Raised if the CSS value doesn't contain a float value.
public GetFloatValue ( CssPrimitiveType unitType ) : double
unitType CssPrimitiveType A unit code to get the float value. The unit code can only be a float unit type (i.e. CSS_NUMBER, CSS_PERCENTAGE, CSS_EMS, CSS_EXS, CSS_PX, CSS_CM, CSS_MM, CSS_IN, CSS_PT, CSS_PC, CSS_DEG, CSS_RAD, CSS_GRAD, CSS_MS, CSS_S, CSS_HZ, CSS_KHZ, CSS_DIMENSION).
return double
        public virtual double GetFloatValue(CssPrimitiveType unitType)
        {
            if(double.IsNaN(floatValue))
            {
                throw new DomException(DomExceptionType.InvalidAccessErr);
            }
            else
            {
                double ret = double.NaN;
                switch(PrimitiveType)
                {
                    case CssPrimitiveType.Percentage:
                        if(unitType == CssPrimitiveType.Percentage) ret = floatValue;
                        break;
                    case CssPrimitiveType.Ms:
                        if(unitType == CssPrimitiveType.Ms) ret = floatValue;
                        else if(unitType == CssPrimitiveType.S) ret = floatValue / 1000;
                        break;
                    case CssPrimitiveType.S:
                        if(unitType == CssPrimitiveType.Ms) ret = floatValue * 1000;
                        else if(unitType == CssPrimitiveType.S) ret = floatValue;
                        break;
                    case CssPrimitiveType.Hz:
                        if(unitType == CssPrimitiveType.Hz) ret = floatValue;
                        else if(unitType == CssPrimitiveType.KHz) ret = floatValue / 1000;
                        break;
                    case CssPrimitiveType.KHz:
                        if(unitType == CssPrimitiveType.Hz) ret = floatValue * 1000;
                        else if(unitType == CssPrimitiveType.KHz) ret = floatValue;
                        break;
                    case CssPrimitiveType.Dimension:
                        if(unitType == CssPrimitiveType.Dimension) ret = floatValue;
                        break;
                }
                if(double.IsNaN(ret))
                {
                    throw new DomException(DomExceptionType.InvalidAccessErr);
                }
                else return ret;
            }
        }

Usage Example

示例#1
0
        public void TestInheritEm()
        {
            CssStyleDeclaration csd       = getStyles("<a />", "root{font-size:18px} a{inher:3em}", "a");
            CssPrimitiveValue   primValue = csd.GetPropertyCssValue("inher") as CssPrimitiveValue;

            Assert.AreEqual(54, primValue.GetFloatValue(CssPrimitiveType.Px));
        }
All Usage Examples Of SharpVectors.Dom.Css.CssPrimitiveValue::GetFloatValue