Class: OraNumber

Inherits:
Numeric show all
Includes:
Comparable
Defined in:
ext/oci8/ocinumber.c,
lib/oci8/oci8.rb,
ext/oci8/ocinumber.c

Overview

OraNumber is a ruby representation of Oracle NUMBER data type. without precision and scale designators.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expr = nil, fmt = nil, nlsparam = nil) ⇒ Object

Creates a value of OraNumber from expr. The expr can be a Numeric value or a String value. If it is a String value, optional fmt and nlsparam is used as Oracle SQL function TO_NUMBER does.

Examples:

# Numeric expr
OraNumber.new(123456.789)   # -> 123456.789
# String expr
OraNumber.new('123456.789') # => #<OraNumber:123456.789>
# String expr with fmt
OraNumber.new('123,456.789', '999,999,999.999') # => #<OraNumber:123456.789>
# String expr with fmt and nlsparam
OraNumber.new('123.456,789', '999G999G999D999', "NLS_NUMERIC_CHARACTERS = ',.'") # => #<OraNumber:123456.789>

Parameters:



812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# File 'ext/oci8/ocinumber.c', line 812

static VALUE onum_initialize(int argc, VALUE *argv, VALUE self)
{
    OCIError *errhp = oci8_errhp;
    VALUE val;
    VALUE fmt;
    VALUE nls_params;

    if (rb_scan_args(argc, argv, "03", &val /* 0 */, &fmt /* nil */, &nls_params /* nil */) == 0) {
        OCINumberSetZero(errhp, _NUMBER(self));
    } else if (RTEST(rb_obj_is_kind_of(val, rb_cNumeric))) {
        set_oci_number_from_num(_NUMBER(self), val, 1, errhp);
    } else {
        set_oci_number_from_str(_NUMBER(self), val, fmt, nls_params, errhp);
    }
    return Qnil;
}

Class Method Details

._load(bytes) ⇒ OraNumber

Restores a byte stream serialized by #_dump. This method is called by Marshal.load() to deserialize a byte stream created by Marshal.dump().

Parameters:

  • bytes (String)

    a byte stream

Returns:



1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
# File 'ext/oci8/ocinumber.c', line 1651

static VALUE
onum_s_load(VALUE klass, VALUE str)
{
    unsigned char *c;
    size_t size;
    OCINumber num;

    Check_Type(str, T_STRING);
    c = RSTRING_ORATEXT(str);
    size = RSTRING_LEN(str);
    if (size == 0 || size != c[0] + 1u || size > sizeof(num)) {
        rb_raise(rb_eTypeError, "marshaled OCI::Number format differ");
    }
    memset(&num, 0, sizeof(num));
    memcpy(&num, c, size);
    return oci8_make_ocinumber(&num, oci8_errhp);
}

Instance Method Details

#%(other) ⇒ OraNumber

Returns the modulo after division of self by other.

Examples:

OraNumber(13) % 5 # => #<OraNumber:3>

Parameters:

Returns:

Raises:

  • (ZeroDivisionError)

    when other is zero.



1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
# File 'ext/oci8/ocinumber.c', line 1080

static VALUE onum_mod(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    OCINumber r;
    boolean is_zero;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0, errhp))
        return rb_num_coerce_bin(lhs, rhs, '%');
    /* check whether argument is not zero. */
    chkerr(OCINumberIsZero(errhp, &n, &is_zero));
    if (is_zero)
        rb_num_zerodiv();
    /* modulo */
    chkerr(OCINumberMod(errhp, _NUMBER(lhs), &n, &r));
    return oci8_make_ocinumber(&r, errhp);
}

#*(other) ⇒ Numeric

Returns the product of self and other. When other’s class is Integer, it returns an OraNumber value. Otherwise, it returns a value same with other’s class.

Examples:

OraNumber(2) * 3   # => #<OraNumber:6>
OraNumber(2) * 1.5 # => 3.0 (Float)

Parameters:

Returns:



991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
# File 'ext/oci8/ocinumber.c', line 991

static VALUE onum_mul(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    OCINumber r;

    switch (rboci8_type(rhs)) {
    case T_FIXNUM:
    case T_BIGNUM:
        if (set_oci_number_from_num(&n, rhs, 0, errhp)) {
            chkerr(OCINumberMul(errhp, _NUMBER(lhs), &n, &r));
            return oci8_make_ocinumber(&r, errhp);
        }
        break;
    case RBOCI8_T_ORANUMBER:
        chkerr(OCINumberMul(errhp, _NUMBER(lhs), _NUMBER(rhs), &r));
        return oci8_make_ocinumber(&r, errhp);
    case T_FLOAT:
        return rb_funcall(onum_to_f(lhs), oci8_id_mul_op, 1, rhs);
    case RBOCI8_T_RATIONAL:
        return rb_funcall(onum_to_r(lhs), oci8_id_mul_op, 1, rhs);
    case RBOCI8_T_BIGDECIMAL:
        return rb_funcall(onum_to_d(lhs), oci8_id_mul_op, 1, rhs);
    }
    return rb_num_coerce_bin(lhs, rhs, oci8_id_mul_op);
}

#**(other) ⇒ OraNumber

Raises self to the power of other.

Examples:

OraNumber(2) ** 2   # => #<OraNumber:4>
OraNumber(2) ** 2.5 # => #<OraNumber:5.65685424949238019520675489683879231435>

Parameters:

Returns:



1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
# File 'ext/oci8/ocinumber.c', line 1111

static VALUE onum_power(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    OCINumber r;

    if (FIXNUM_P(rhs)) {
        chkerr(OCINumberIntPower(errhp, _NUMBER(lhs), FIX2INT(rhs), &r));
    } else {
        /* change to OCINumber */
        if (!set_oci_number_from_num(&n, rhs, 0, errhp))
            return rb_num_coerce_bin(lhs, rhs, id_power);
        chkerr(OCINumberPower(errhp, _NUMBER(lhs), &n, &r));
    }
    return oci8_make_ocinumber(&r, errhp);
}

#+(other) ⇒ Numeric

Returns the sum of self and other. When other’s class is Integer, it returns an OraNumber value. Otherwise, it returns a value same with other’s class.

Examples:

OraNumber(2) + 3   # => #<OraNumber:5>
OraNumber(2) + 1.5 # => 3.5 (Float)

Parameters:

Returns:



909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
# File 'ext/oci8/ocinumber.c', line 909

static VALUE onum_add(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    OCINumber r;

    switch (rboci8_type(rhs)) {
    case T_FIXNUM:
    case T_BIGNUM:
        if (set_oci_number_from_num(&n, rhs, 0, errhp)) {
            chkerr(OCINumberAdd(errhp, _NUMBER(lhs), &n, &r));
            return oci8_make_ocinumber(&r, errhp);
        }
        break;
    case RBOCI8_T_ORANUMBER:
        chkerr(OCINumberAdd(errhp, _NUMBER(lhs), _NUMBER(rhs), &r));
        return oci8_make_ocinumber(&r, errhp);
    case T_FLOAT:
        return rb_funcall(onum_to_f(lhs), oci8_id_add_op, 1, rhs);
    case RBOCI8_T_RATIONAL:
        return rb_funcall(onum_to_r(lhs), oci8_id_add_op, 1, rhs);
    case RBOCI8_T_BIGDECIMAL:
        return rb_funcall(onum_to_d(lhs), oci8_id_add_op, 1, rhs);
    }
    return rb_num_coerce_bin(lhs, rhs, oci8_id_add_op);
}

#-(other) ⇒ Numeric

Returns the difference of self and other. When other’s class is Integer, it returns an OraNumber value. Otherwise, it returns a value same with other’s class.

Examples:

OraNumber(2) - 3   # => #<OraNumber:-1>
OraNumber(2) - 1.5 # => 0.5 (Float)

Parameters:

Returns:



950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
# File 'ext/oci8/ocinumber.c', line 950

static VALUE onum_sub(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    OCINumber r;

    switch (rboci8_type(rhs)) {
    case T_FIXNUM:
    case T_BIGNUM:
        if (set_oci_number_from_num(&n, rhs, 0, errhp)) {
            chkerr(OCINumberSub(errhp, _NUMBER(lhs), &n, &r));
            return oci8_make_ocinumber(&r, errhp);
        }
        break;
    case RBOCI8_T_ORANUMBER:
        chkerr(OCINumberSub(errhp, _NUMBER(lhs), _NUMBER(rhs), &r));
        return oci8_make_ocinumber(&r, errhp);
    case T_FLOAT:
        return rb_funcall(onum_to_f(lhs), oci8_id_sub_op, 1, rhs);
    case RBOCI8_T_RATIONAL:
        return rb_funcall(onum_to_r(lhs), oci8_id_sub_op, 1, rhs);
    case RBOCI8_T_BIGDECIMAL:
        return rb_funcall(onum_to_d(lhs), oci8_id_sub_op, 1, rhs);
    }
    return rb_num_coerce_bin(lhs, rhs, oci8_id_sub_op);
}

#-@OraNumber

Returns a negated value of self

Examples:

-OraNumber(2)  # =>  #<OraNumber:-2>

Returns:



886
887
888
889
890
891
892
893
# File 'ext/oci8/ocinumber.c', line 886

static VALUE onum_neg(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    OCINumber r;

    chkerr(OCINumberNeg(errhp, _NUMBER(self), &r));
    return oci8_make_ocinumber(&r, errhp);
}

#/(other) ⇒ Numeric

Returns the result of dividing self by other. When other’s class is Integer, it returns an OraNumber value. Otherwise, it returns a value same with other’s class.

Examples:

OraNumber(2) / 3   # => #<OraNumber:0.6666666666666666666666666666666666666667>
OraNumber(2) / 1.5 # => 1.3333333333333333 (Float)

Parameters:

Returns:



1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
# File 'ext/oci8/ocinumber.c', line 1032

static VALUE onum_div(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    OCINumber r;
    boolean is_zero;

    switch (rboci8_type(rhs)) {
    case T_FIXNUM:
        if (rhs == INT2FIX(0)) {
            rb_num_zerodiv();
        }
    case T_BIGNUM:
        if (set_oci_number_from_num(&n, rhs, 0, errhp)) {
            chkerr(OCINumberDiv(errhp, _NUMBER(lhs), &n, &r));
            return oci8_make_ocinumber(&r, errhp);
        }
        break;
    case RBOCI8_T_ORANUMBER:
        chkerr(OCINumberIsZero(errhp, _NUMBER(rhs), &is_zero));
        if (is_zero) {
            rb_num_zerodiv();
        }
        chkerr(OCINumberDiv(errhp, _NUMBER(lhs), _NUMBER(rhs), &r));
        return oci8_make_ocinumber(&r, errhp);
    case T_FLOAT:
        return rb_funcall(onum_to_f(lhs), oci8_id_div_op, 1, rhs);
    case RBOCI8_T_RATIONAL:
        return rb_funcall(onum_to_r(lhs), oci8_id_div_op, 1, rhs);
    case RBOCI8_T_BIGDECIMAL:
        return rb_funcall(onum_to_d(lhs), oci8_id_div_op, 1, rhs);
    }
    return rb_num_coerce_bin(lhs, rhs, oci8_id_div_op);
}

#<=>(other) ⇒ -1, 0 or +1

Returns -1, 0, or +1 depending on whether self is less than, equal to, or greater than other. This is the basis for the tests in Comparable.

Examples:

OraNumber(5) <=> 3   # => 1
OraNumber(4) <=> 4   # => 0
OraNumber(2) <=> 2.5 # => 1

Parameters:

Returns:

  • (-1, 0 or +1)


1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
# File 'ext/oci8/ocinumber.c', line 1143

static VALUE onum_cmp(VALUE lhs, VALUE rhs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber n;
    sword r;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0, errhp))
        return rb_num_coerce_cmp(lhs, rhs, id_cmp);
    /* compare */
    chkerr(OCINumberCmp(errhp, _NUMBER(lhs), &n, &r));
    if (r > 0) {
        return INT2FIX(1);
    } else if (r == 0) {
        return INT2FIX(0);
    } else {
        return INT2FIX(-1);
    }
}

#_dumpString

Serializes self. This method is called by Marshal.dump().

Returns:

See Also:



1631
1632
1633
1634
1635
1636
1637
1638
1639
# File 'ext/oci8/ocinumber.c', line 1631

static VALUE onum__dump(int argc, VALUE *argv, VALUE self)
{
    char *c  = RTYPEDDATA_DATA(self);
    int size = c[0] + 1;
    VALUE dummy;

    rb_scan_args(argc, argv, "01", &dummy);
    return rb_str_new(c, size);
}

#absOraNumber

Returns the absolute value of self.

Returns:



1535
1536
1537
1538
1539
1540
1541
1542
# File 'ext/oci8/ocinumber.c', line 1535

static VALUE onum_abs(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    OCINumber result;

    chkerr(OCINumberAbs(errhp, _NUMBER(self), &result));
    return oci8_make_ocinumber(&result, errhp);
}

#ceilInteger

Returns the smallest Integer greater than or equal to self.

Examples:

OraNumber(11.1).ceil  # => 12
OraNumber(25.8).ceil  # => 26
OraNumber(-25.8).ceil # => -25

Returns:

  • (Integer)


1196
1197
1198
1199
1200
1201
1202
1203
# File 'ext/oci8/ocinumber.c', line 1196

static VALUE onum_ceil(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    OCINumber r;

    chkerr(OCINumberCeil(errhp, _NUMBER(self), &r));
    return oci8_make_integer(&r, errhp);
}

#dumpString

Returns internal representation whose format is same with the return value of Oracle SQL function DUMP.

Examples:

OraNumber.new(100).dump  #=> "Typ=2 Len=2: 194,2"
OraNumber.new(123).dump  #=> "Typ=2 Len=3: 194,2,24"
OraNumber.new(0.1).dump  #=> "Typ=2 Len=2: 192,11"

Returns:

Since:

  • 2.0.4



1580
1581
1582
1583
1584
1585
# File 'ext/oci8/ocinumber.c', line 1580

static VALUE onum_dump(VALUE self)
{
    char buf[ORANUMBER_DUMP_BUF_SIZ];
    int rv = oranumber_dump(_NUMBER(self), buf);
    return rb_usascii_str_new(buf, rv);
}

#floorInteger

Returns the largest Integer less than or equal to self.

Examples:

OraNumber(11.1).floor  # => 11
OraNumber(25.8).floor  # => 25
OraNumber(-25.8).floor # => -26

Returns:

  • (Integer)


1175
1176
1177
1178
1179
1180
1181
1182
# File 'ext/oci8/ocinumber.c', line 1175

static VALUE onum_floor(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    OCINumber r;

    chkerr(OCINumberFloor(errhp, _NUMBER(self), &r));
    return oci8_make_integer(&r, errhp);
}

#has_decimal_part?Boolean

Returns true if self has a decimal part.

Examples:

OraNumber(10).has_decimal_part?   # => false
OraNumber(10.1).has_decimal_part? # => true

Since:

  • 2.0.5

Returns:

  • (Boolean)


1492
1493
1494
1495
1496
1497
1498
1499
# File 'ext/oci8/ocinumber.c', line 1492

static VALUE onum_has_decimal_part_p(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    boolean result;

    chkerr(OCINumberIsInt(errhp, _NUMBER(self), &result));
    return result ? Qfalse : Qtrue;
}

#initialize_copy(obj) ⇒ Object

Replaces self with obj. Object#clone and Object#dup call this method to copy data unknown by the ruby interpreter.

Parameters:



839
840
841
842
843
844
845
846
847
# File 'ext/oci8/ocinumber.c', line 839

static VALUE onum_initialize_copy(VALUE lhs, VALUE rhs)
{
    if (!RTEST(rb_obj_is_instance_of(rhs, CLASS_OF(lhs)))) {
        rb_raise(rb_eTypeError, "invalid type: expected %s but %s",
                 rb_class2name(CLASS_OF(lhs)), rb_class2name(CLASS_OF(rhs)));
    }
    chkerr(OCINumberAssign(oci8_errhp, _NUMBER(rhs), _NUMBER(lhs)));
    return lhs;
}

#roundInteger #round(decplace) ⇒ OraNumber

Overloads:

  • #roundInteger

    Rounds self to the nearest Integer.

    Examples:

    OraNumber(1.49).round     # => 1
    OraNumber(1.5).round      # => 2
    OraNumber(-1.49).round    # => -1
    OraNumber(-1.5).round     # => -2
    

    Returns:

    • (Integer)
  • #round(decplace) ⇒ OraNumber

    Rounds onum to a specified decimal place decplace.

    Examples:

    OraNumber(123.456).round(2)   # => #<OraNumber:123.46>
    OraNumber(123.456).round(1)   # => #<OraNumber:123.5>
    OraNumber(123.456).round(0)   # => #<OraNumber:123>
    OraNumber(123.456).round(-1)  # => #<OraNumber:120>
    

    Parameters:

    • decplace (Integer)

    Returns:



1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
# File 'ext/oci8/ocinumber.c', line 1231

static VALUE onum_round(int argc, VALUE *argv, VALUE self)
{
    OCIError *errhp = oci8_errhp;
    VALUE decplace;
    OCINumber r;

    rb_scan_args(argc, argv, "01", &decplace /* 0 */);
    chkerr(OCINumberRound(errhp, _NUMBER(self), NIL_P(decplace) ? 0 : NUM2INT(decplace), &r));
    if (argc == 0) {
        return oci8_make_integer(&r, errhp);
    } else {
        return oci8_make_ocinumber(&r, errhp);
    }
}

#round_prec(digits) ⇒ OraNumber

Rounds self to a specified number of decimal digits. This method is available on Oracle 8.1 client or upper.

Examples:

OraNumber(1.234).round_prec(2) # => #<OraNumber:1.2>
OraNumber(12.34).round_prec(2) # => #<OraNumber:12>
OraNumber(123.4).round_prec(2) # => #<OraNumber:120>

Parameters:

  • digits (Integer)

Returns:



1291
1292
1293
1294
1295
1296
1297
1298
# File 'ext/oci8/ocinumber.c', line 1291

static VALUE onum_round_prec(VALUE self, VALUE ndigs)
{
    OCIError *errhp = oci8_errhp;
    OCINumber r;

    chkerr(OCINumberPrec(errhp, _NUMBER(self), NUM2INT(ndigs), &r));
    return oci8_make_ocinumber(&r, errhp);
}

#shift(ndigits) ⇒ OraNumber

Returns self shifted by ndigits This method is available on Oracle 8.1 client or upper.

Examples:

OraNumber(123).shift(3)  # => #<OraNumber:123000>
OraNumber(123).shift(-3) # => #<OraNumber:0.123>

Parameters:

  • ndigits (Integer)

Returns:



1557
1558
1559
1560
1561
1562
1563
1564
# File 'ext/oci8/ocinumber.c', line 1557

static VALUE onum_shift(VALUE self, VALUE exp)
{
    OCIError *errhp = oci8_errhp;
    OCINumber result;

    chkerr(OCINumberShift(errhp, _NUMBER(self), NUM2INT(exp), &result));
    return oci8_make_ocinumber(&result, errhp);
}

#onum.to_char(fmt = nil, nlsparam = nil) ⇒ String

Returns a string containing a representation of self. fmt and nlsparam are used as Oracle SQL function TO_CHAR(number) does.

Examples:

OraNumber(123456.789).to_char('FM999,999,999.999')  # => "123,456.789"
OraNumber(123456.789).to_char('FM999G999G999D999',  "NLS_NUMERIC_CHARACTERS = ',.'") # => "123.456,789"

Parameters:

  • fmt (String) (defaults to: nil)
  • nlsparam (String) (defaults to: nil)

Returns:



1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
# File 'ext/oci8/ocinumber.c', line 1316

static VALUE onum_to_char(int argc, VALUE *argv, VALUE self)
{
    OCIError *errhp = oci8_errhp;
    VALUE fmt;
    VALUE nls_params;
    char buf[512];
    ub4 buf_size = sizeof(buf);
    oratext *fmt_ptr;
    oratext *nls_params_ptr;
    ub4 fmt_len;
    ub4 nls_params_len;
    sword rv;

    rb_scan_args(argc, argv, "02", &fmt /* nil */, &nls_params /* nil */);
    if (NIL_P(fmt)) {
        rv = oranumber_to_str(_NUMBER(self), buf, sizeof(buf));
        if (rv > 0) {
            return rb_usascii_str_new(buf, rv);
        }
        oranumber_dump(_NUMBER(self), buf);
        rb_raise(eOCIException, "Invalid internal number format: %s", buf);
    }
    StringValue(fmt);
    fmt_ptr = RSTRING_ORATEXT(fmt);
    fmt_len = RSTRING_LEN(fmt);
    if (NIL_P(nls_params)) {
        nls_params_ptr = NULL;
        nls_params_len = 0;
    } else {
        StringValue(nls_params);
        nls_params_ptr = RSTRING_ORATEXT(nls_params);
        nls_params_len = RSTRING_LEN(nls_params);
    }
    rv = OCINumberToText(errhp, _NUMBER(self),
                         fmt_ptr, fmt_len, nls_params_ptr, nls_params_len,
                         &buf_size, TO_ORATEXT(buf));
    if (rv == OCI_ERROR) {
        sb4 errcode;
        OCIErrorGet(errhp, 1, NULL, &errcode, NULL, 0, OCI_HTYPE_ERROR);
        if (errcode == 22065) {
            /* OCI-22065: number to text translation for the given format causes overflow */
            if (NIL_P(fmt)) /* implicit conversion */
                return rb_usascii_str_new_cstr("overflow");
        }
        chkerr(rv);
    }
    return rb_usascii_str_new(buf, buf_size);
}

#to_dBigDecimal

Returns self as a BigDecimal.

Returns:

  • (BigDecimal)


1460
1461
1462
1463
# File 'ext/oci8/ocinumber.c', line 1460

static VALUE onum_to_d(VALUE self)
{
    return onum_to_d_real(_NUMBER(self), oci8_errhp);
}

#to_fFloat

Converts self to Float.

When OCI8.properties [:float_conversion_type] is :ruby, self is converted by self.to_s.to_f. When it is :oracle, self is converted by the Oracle OCI function OCINumberToReal().

Returns:

  • (Float)


1405
1406
1407
1408
# File 'ext/oci8/ocinumber.c', line 1405

static VALUE onum_to_f(VALUE self)
{
    return rb_float_new(oci8_onum_to_dbl(_NUMBER(self), oci8_errhp));
}

#to_iInteger

Returns self truncated to an Integer.

Returns:

  • (Integer)


1386
1387
1388
1389
1390
1391
1392
1393
# File 'ext/oci8/ocinumber.c', line 1386

static VALUE onum_to_i(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    OCINumber num;

    chkerr(OCINumberTrunc(errhp, _NUMBER(self), 0, &num));
    return oci8_make_integer(&num, errhp);
}

#to_onumOraNumber

Returns self.

Returns:



1508
1509
1510
1511
# File 'ext/oci8/ocinumber.c', line 1508

static VALUE onum_to_onum(VALUE self)
{
    return self;
}

#to_rRational

Returns self as a Rational.

Returns:

  • (Rational)


1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
# File 'ext/oci8/ocinumber.c', line 1417

static VALUE onum_to_r(VALUE self)
{
    VALUE x, y;
    int nshift = 0;
    OCINumber onum[2];
    int current = 0;
    boolean is_int;

    chkerr(OCINumberAssign(oci8_errhp, _NUMBER(self), &onum[0]));

    for (;;) {
        chkerr(OCINumberIsInt(oci8_errhp, &onum[current], &is_int));
        if (is_int) {
            break;
        }
        nshift++;
        chkerr(OCINumberShift(oci8_errhp, &onum[current], 1, &onum[1 - current]));
        current = 1 - current;
    }
    x = oci8_make_integer(&onum[current], oci8_errhp);
    if (nshift == 0) {
        y = INT2FIX(1);
    } else {
        y = rb_funcall(INT2FIX(10), rb_intern("**"), 1, INT2FIX(nshift));
    }
#ifdef rb_Rational2
    return rb_Rational(x, y);
#else
    if (!cRational) {
        rb_require("rational");
        cRational = rb_const_get(rb_cObject, id_Rational);
    }
    return rb_funcall(rb_cObject, id_Rational, 2, x, y);
#endif
}

#to_sString

Returns a string containing a representation of self.

Returns:

See Also:



1374
1375
1376
1377
# File 'ext/oci8/ocinumber.c', line 1374

static VALUE onum_to_s(VALUE self)
{
    return onum_to_char(0, NULL, self);
}

#truncate(decplace = 0) ⇒ OraNumber

TODO:

returns Integer when decplace is not specified.

Truncates self to a specified decimal place decplace.

Examples:

OraNumber(123.456).truncate      # => #<OraNumber:123>
OraNumber(123.456).truncate(1)   # => #<OraNumber:123.4>
OraNumber(123.456).truncate(2)   # => #<OraNumber:123.45>
OraNumber(123.456).truncate(-1)  # => #<OraNumber:120>

OraNumber(-123.456).truncate     # => #<OraNumber:-123>
OraNumber(-123.456).truncate(1)  # => #<OraNumber:-123.4>
OraNumber(-123.456).truncate(2)  # => #<OraNumber:-123.45>
OraNumber(-123.456).truncate(-1) # => #<OraNumber:-120>

Parameters:

  • decplace (Integer) (defaults to: 0)

Returns:



1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
# File 'ext/oci8/ocinumber.c', line 1266

static VALUE onum_trunc(int argc, VALUE *argv, VALUE self)
{
    OCIError *errhp = oci8_errhp;
    VALUE decplace;
    OCINumber r;

    rb_scan_args(argc, argv, "01", &decplace /* 0 */);
    chkerr(OCINumberTrunc(errhp, _NUMBER(self), NIL_P(decplace) ? 0 : NUM2INT(decplace), &r));
    return oci8_make_ocinumber(&r, errhp);
}

#zero?Boolean

Returns true if self is zero.

Returns:

  • (Boolean)


1519
1520
1521
1522
1523
1524
1525
1526
# File 'ext/oci8/ocinumber.c', line 1519

static VALUE onum_zero_p(VALUE self)
{
    OCIError *errhp = oci8_errhp;
    boolean result;

    chkerr(OCINumberIsZero(errhp, _NUMBER(self), &result));
    return result ? Qtrue : Qfalse;
}