double Value::asDouble() const { CCASSERT(_type != Type::VECTOR && _type != Type::MAP && _type != Type::INT_KEY_MAP, "Only base type (bool, string, float, double, int) could be converted"); if (_type == Type::DOUBLE) { return _field.doubleVal; } if (_type == Type::BYTE) { return static_cast<double>(_field.byteVal); } if (_type == Type::STRING) { return static_cast<double>(atof2(_field.strVal->c_str())); } if (_type == Type::INTEGER) { return static_cast<double>(_field.intVal); } if (_type == Type::FLOAT) { return static_cast<double>(_field.floatVal); } if (_type == Type::BOOLEAN) { return _field.boolVal ? 1.0 : 0.0; } return 0.0; }
main() { char line[MAXLINE]; printf("Type in a value of the form 123.45e-6\n"); /* checking my version of atof matches up with <stdio.h>'s version */ while (getline(line, MAXLINE) > 0) { printf("\t%g\n", atof2(line)); /* my atof */ printf("\t%g\n", atof(line)); /* c's atof */ } return 0; }
/* atoi: convert string s to integer using atof */ int atoi2(char s[]) { //double atof(char s[]); return (int)atof2(s); }