当前位置: 首页>>代码示例>>C++>>正文


C++ TiValue::getJSNumber方法代码示例

本文整理汇总了C++中TiValue::getJSNumber方法的典型用法代码示例。如果您正苦于以下问题:C++ TiValue::getJSNumber方法的具体用法?C++ TiValue::getJSNumber怎么用?C++ TiValue::getJSNumber使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TiValue的用法示例。


在下文中一共展示了TiValue::getJSNumber方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: numberProtoFuncToPrecision

// toPrecision converts a number to a string, takeing an argument specifying a
// number of significant figures to round the significand to. For positive
// exponent, all values that can be represented using a decimal fraction will
// be, e.g. when rounding to 3 s.f. any value up to 999 will be formated as a
// decimal, whilst 1000 is converted to the exponential representation 1.00e+3.
// For negative exponents values >= 1e-6 are formated as decimal fractions,
// with smaller values converted to exponential representation.
EncodedTiValue JSC_HOST_CALL numberProtoFuncToPrecision(TiExcState* exec)
{
    // Get x (the double value of this, which should be a Number).
    TiValue thisValue = exec->hostThisValue();
    TiValue v = thisValue.getJSNumber();
    if (!v)
        return throwVMTypeError(exec);
    double x = v.uncheckedGetNumber();

    // Get the argument. 
    int significantFigures;
    bool isUndefined;
    if (!getIntegerArgumentInRange(exec, 1, 21, significantFigures, isUndefined))
        return throwVMError(exec, createRangeError(exec, "toPrecision() argument must be between 1 and 21"));

    // To precision called with no argument is treated as ToString.
    if (isUndefined)
        return TiValue::encode(jsString(exec, UString::number(x)));

    // Handle NaN and Infinity.
    if (isnan(x) || isinf(x))
        return TiValue::encode(jsString(exec, UString::number(x)));

    // Convert to decimal with rounding.
    DecimalNumber number(x, RoundingSignificantFigures, significantFigures);
    // If number is in the range 1e-6 <= x < pow(10, significantFigures) then format
    // as decimal. Otherwise, format the number as an exponential.  Decimal format
    // demands a minimum of (exponent + 1) digits to represent a number, for example
    // 1234 (1.234e+3) requires 4 digits. (See ECMA-262 15.7.4.7.10.c)
    NumberToStringBuffer buffer;
    unsigned length = number.exponent() >= -6 && number.exponent() < significantFigures
        ? number.toStringDecimal(buffer, WTI::NumberToStringBufferLength)
        : number.toStringExponential(buffer, WTI::NumberToStringBufferLength);
    return TiValue::encode(jsString(exec, UString(buffer, length)));
}
开发者ID:AngelkPetkov,项目名称:tijscore,代码行数:42,代码来源:NumberPrototype.cpp

示例2: numberProtoFuncToFixed

// toFixed converts a number to a string, always formatting as an a decimal fraction.
// This method takes an argument specifying a number of decimal places to round the
// significand to. However when converting large values (1e+21 and above) this
// method will instead fallback to calling ToString. 
EncodedTiValue JSC_HOST_CALL numberProtoFuncToFixed(TiExcState* exec)
{
    // Get x (the double value of this, which should be a Number).
    TiValue thisValue = exec->hostThisValue();
    TiValue v = thisValue.getJSNumber();
    if (!v)
        return throwVMTypeError(exec);
    double x = v.uncheckedGetNumber();

    // Get the argument. 
    int decimalPlaces;
    bool isUndefined; // This is ignored; undefined treated as 0.
    if (!getIntegerArgumentInRange(exec, 0, 20, decimalPlaces, isUndefined))
        return throwVMError(exec, createRangeError(exec, "toFixed() argument must be between 0 and 20"));

    // 15.7.4.5.7 states "If x >= 10^21, then let m = ToString(x)"
    // This also covers Ininity, and structure the check so that NaN
    // values are also handled by numberToString
    if (!(fabs(x) < 1e+21))
        return TiValue::encode(jsString(exec, UString::number(x)));

    // The check above will return false for NaN or Infinity, these will be
    // handled by numberToString.
    ASSERT(!isnan(x) && !isinf(x));

    // Convert to decimal with rounding, and format as decimal.
    NumberToStringBuffer buffer;
    unsigned length = DecimalNumber(x, RoundingDecimalPlaces, decimalPlaces).toStringDecimal(buffer, WTI::NumberToStringBufferLength);
    return TiValue::encode(jsString(exec, UString(buffer, length)));
}
开发者ID:AngelkPetkov,项目名称:tijscore,代码行数:34,代码来源:NumberPrototype.cpp

示例3: toThisNumber

static ALWAYS_INLINE bool toThisNumber(TiValue thisValue, double &x)
{
    TiValue v = thisValue.getJSNumber();
    if (UNLIKELY(!v))
        return false;
    x = v.uncheckedGetNumber();
    return true;
}
开发者ID:AngelkPetkov,项目名称:tijscore,代码行数:8,代码来源:NumberPrototype.cpp

示例4: numberProtoFuncValueOf

EncodedTiValue JSC_HOST_CALL numberProtoFuncValueOf(TiExcState* exec)
{
    TiValue thisValue = exec->hostThisValue();
    TiValue v = thisValue.getJSNumber();
    if (!v)
        return throwVMTypeError(exec);

    return TiValue::encode(v);
}
开发者ID:AngelkPetkov,项目名称:tijscore,代码行数:9,代码来源:NumberPrototype.cpp

示例5: numberProtoFuncToLocaleString

EncodedTiValue JSC_HOST_CALL numberProtoFuncToLocaleString(TiExcState* exec)
{
    TiValue thisValue = exec->hostThisValue();
    // FIXME: Not implemented yet.

    TiValue v = thisValue.getJSNumber();
    if (!v)
        return throwVMTypeError(exec);

    return TiValue::encode(jsString(exec, v.toString(exec)));
}
开发者ID:AngelkPetkov,项目名称:tijscore,代码行数:11,代码来源:NumberPrototype.cpp

示例6: numberProtoFuncToString

EncodedTiValue JSC_HOST_CALL numberProtoFuncToString(TiExcState* exec)
{
    TiValue thisValue = exec->hostThisValue();
    TiValue v = thisValue.getJSNumber();
    if (!v)
        return throwVMTypeError(exec);

    TiValue radixValue = exec->argument(0);
    int radix;
    if (radixValue.isInt32())
        radix = radixValue.asInt32();
    else if (radixValue.isUndefined())
        radix = 10;
    else
        radix = static_cast<int>(radixValue.toInteger(exec)); // nan -> 0

    if (radix == 10)
        return TiValue::encode(jsString(exec, v.toString(exec)));

    static const char* const digits = "0123456789abcdefghijklmnopqrstuvwxyz";

    // Fast path for number to character conversion.
    if (radix == 36) {
        if (v.isInt32()) {
            int x = v.asInt32();
            if (static_cast<unsigned>(x) < 36) { // Exclude negatives
                TiGlobalData* globalData = &exec->globalData();
                return TiValue::encode(globalData->smallStrings.singleCharacterString(globalData, digits[x]));
            }
        }
    }

    if (radix < 2 || radix > 36)
        return throwVMError(exec, createRangeError(exec, "toString() radix argument must be between 2 and 36"));

    // INT_MAX results in 1024 characters left of the dot with radix 2
    // give the same space on the right side. safety checks are in place
    // unless someone finds a precise rule.
    char s[2048 + 3];
    const char* lastCharInString = s + sizeof(s) - 1;
    double x = v.uncheckedGetNumber();
    if (isnan(x) || isinf(x))
        return TiValue::encode(jsString(exec, UString::number(x)));

    bool isNegative = x < 0.0;
    if (isNegative)
        x = -x;

    double integerPart = floor(x);
    char* decimalPoint = s + sizeof(s) / 2;

    // convert integer portion
    char* p = decimalPoint;
    double d = integerPart;
    do {
        int remainderDigit = static_cast<int>(fmod(d, radix));
        *--p = digits[remainderDigit];
        d /= radix;
    } while ((d <= -1.0 || d >= 1.0) && s < p);

    if (isNegative)
        *--p = '-';
    char* startOfResultString = p;
    ASSERT(s <= startOfResultString);

    d = x - integerPart;
    p = decimalPoint;
    const double epsilon = 0.001; // TODO: guessed. base on radix ?
    bool hasFractionalPart = (d < -epsilon || d > epsilon);
    if (hasFractionalPart) {
        *p++ = '.';
        do {
            d *= radix;
            const int digit = static_cast<int>(d);
            *p++ = digits[digit];
            d -= digit;
        } while ((d < -epsilon || d > epsilon) && p < lastCharInString);
    }
    *p = '\0';
    ASSERT(p < s + sizeof(s));

    return TiValue::encode(jsString(exec, startOfResultString));
}
开发者ID:AngelkPetkov,项目名称:tijscore,代码行数:83,代码来源:NumberPrototype.cpp


注:本文中的TiValue::getJSNumber方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。