Logo Search packages:      
Sourcecode: jruby1.1 version File versions

static RubyFloat org::jruby::RubyNumeric::str2fnum ( Ruby  runtime,
RubyString  arg,
boolean  strict 
) [inline, static, inherited]

Converts a string representation of a floating-point number to the numeric value. Parsing starts at the beginning of the string (after leading and trailing whitespace have been removed), and stops at the end or at the first character that can't be part of a number. If the string fails to parse as a number, 0.0 is returned.

runtime the ruby runtime
arg the string to be converted
strict if true, enforce the strict criteria for String encoding of numeric values, as required by Float('n'), and raise an exception when those criteria are not met. Otherwise, allow lax expression of values, as permitted by String::to_f, and return a value in all cases. TODO: describe the rules/criteria
a RubyFloat representing the result of the conversion, which will be 0.0 if the conversion failed.

Definition at line 347 of file RubyNumeric.java.

References org::jruby::RubyObject::callMethod(), org::jruby::RubyString::getByteList(), org::jruby::Ruby::getCurrentContext(), and org::jruby::Ruby::newArgumentError().

        final double ZERO = 0.0;
        try {
            return new RubyFloat(runtime,Convert.byteListToDouble(arg.getByteList(),strict));
        } catch (NumberFormatException e) {
            if (strict) {
                throw runtime.newArgumentError("invalid value for Float(): "
                        + arg.callMethod(runtime.getCurrentContext(), "inspect").toString());
            return new RubyFloat(runtime,ZERO);

Generated by  Doxygen 1.6.0   Back to index