mirror of
https://github.com/licsber/micropython.git
synced 2024-09-20 09:00:23 +08:00
Allow floating point arguments with %d,i,u,o,x,X formats
This commit is contained in:
parent
a0d32991ed
commit
f81a49e464
17
py/objstr.c
17
py/objstr.c
@ -528,6 +528,15 @@ static bool arg_looks_numeric(mp_obj_t arg) {
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static machine_int_t arg_as_int(mp_obj_t arg) {
|
||||||
|
#if MICROPY_ENABLE_FLOAT
|
||||||
|
if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
|
||||||
|
return mp_obj_get_float(arg);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return mp_obj_get_int(arg);
|
||||||
|
}
|
||||||
|
|
||||||
mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
|
mp_obj_t str_format(uint n_args, const mp_obj_t *args) {
|
||||||
assert(MP_OBJ_IS_STR(args[0]));
|
assert(MP_OBJ_IS_STR(args[0]));
|
||||||
|
|
||||||
@ -991,7 +1000,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
|
|||||||
case 'd':
|
case 'd':
|
||||||
case 'i':
|
case 'i':
|
||||||
case 'u':
|
case 'u':
|
||||||
pfenv_print_int(&pfenv_vstr, mp_obj_get_int(arg), 1, 10, 'a', flags, fill, width);
|
pfenv_print_int(&pfenv_vstr, arg_as_int(arg), 1, 10, 'a', flags, fill, width);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
#if MICROPY_ENABLE_FLOAT
|
#if MICROPY_ENABLE_FLOAT
|
||||||
@ -1009,7 +1018,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
|
|||||||
if (alt) {
|
if (alt) {
|
||||||
flags |= PF_FLAG_SHOW_PREFIX;
|
flags |= PF_FLAG_SHOW_PREFIX;
|
||||||
}
|
}
|
||||||
pfenv_print_int(&pfenv_vstr, mp_obj_get_int(arg), 1, 8, 'a', flags, fill, width);
|
pfenv_print_int(&pfenv_vstr, arg_as_int(arg), 1, 8, 'a', flags, fill, width);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'r':
|
case 'r':
|
||||||
@ -1034,14 +1043,14 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
|
|||||||
if (alt) {
|
if (alt) {
|
||||||
flags |= PF_FLAG_SHOW_PREFIX;
|
flags |= PF_FLAG_SHOW_PREFIX;
|
||||||
}
|
}
|
||||||
pfenv_print_int(&pfenv_vstr, mp_obj_get_int(arg), 1, 16, 'a', flags, fill, width);
|
pfenv_print_int(&pfenv_vstr, arg_as_int(arg), 1, 16, 'a', flags, fill, width);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'X':
|
case 'X':
|
||||||
if (alt) {
|
if (alt) {
|
||||||
flags |= PF_FLAG_SHOW_PREFIX;
|
flags |= PF_FLAG_SHOW_PREFIX;
|
||||||
}
|
}
|
||||||
pfenv_print_int(&pfenv_vstr, mp_obj_get_int(arg), 1, 16, 'A', flags, fill, width);
|
pfenv_print_int(&pfenv_vstr, arg_as_int(arg), 1, 16, 'A', flags, fill, width);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -21,6 +21,14 @@ try:
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
print("TypeError")
|
print("TypeError")
|
||||||
|
|
||||||
|
print("%s" % True)
|
||||||
|
print("%s" % 1)
|
||||||
|
print("%s" % 1.0)
|
||||||
|
|
||||||
|
print("%r" % True)
|
||||||
|
print("%r" % 1)
|
||||||
|
print("%r" % 1.0)
|
||||||
|
|
||||||
print("%c" % 48)
|
print("%c" % 48)
|
||||||
print("%c" % 'a')
|
print("%c" % 'a')
|
||||||
print("%10s" % 'abc')
|
print("%10s" % 'abc')
|
||||||
@ -29,9 +37,20 @@ print("%d" % 10)
|
|||||||
print("%+d" % 10)
|
print("%+d" % 10)
|
||||||
print("% d" % 10)
|
print("% d" % 10)
|
||||||
print("%d" % -10)
|
print("%d" % -10)
|
||||||
|
print("%d" % 1.0)
|
||||||
|
print("%d" % True)
|
||||||
|
print("%i" % -10)
|
||||||
|
print("%i" % 1.0)
|
||||||
|
print("%i" % True)
|
||||||
|
print("%u" % -10)
|
||||||
|
print("%u" % 1.0)
|
||||||
|
print("%u" % True)
|
||||||
print("%x" % 18)
|
print("%x" % 18)
|
||||||
|
print("%x" % 18.0)
|
||||||
print("%o" % 18)
|
print("%o" % 18)
|
||||||
|
print("%o" % 18.0)
|
||||||
print("%X" % 18)
|
print("%X" % 18)
|
||||||
|
print("%X" % 18.0)
|
||||||
print("%#x" % 18)
|
print("%#x" % 18)
|
||||||
print("%#X" % 18)
|
print("%#X" % 18)
|
||||||
print("%#6x" % 18)
|
print("%#6x" % 18)
|
||||||
|
Loading…
Reference in New Issue
Block a user