diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2020-11-18 18:45:38 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2020-11-21 14:38:13 +0900 |
| commit | 1974aa0f723bb1971d46ea2185d6b2f4c6581d55 (patch) | |
| tree | 22a0dbb8cc12a04d01d61491753aa92f1f4f6d9d /src/numeric.c | |
| parent | 5f7eb20256bfd5ba9c7ce4f400401c9253eb8439 (diff) | |
| download | mruby-1974aa0f723bb1971d46ea2185d6b2f4c6581d55.tar.gz mruby-1974aa0f723bb1971d46ea2185d6b2f4c6581d55.zip | |
Update `Float#to_s` to keep trailing zero as CRuby does; ref 68cebb6
Diffstat (limited to 'src/numeric.c')
| -rw-r--r-- | src/numeric.c | 64 |
1 files changed, 18 insertions, 46 deletions
diff --git a/src/numeric.c b/src/numeric.c index fec79ffd1..a16b57dc9 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -249,8 +249,23 @@ flo_div(mrb_state *mrb, mrb_value xv) return mrb_float_value(mrb, x); } +/* 15.2.9.3.16(x) */ +/* + * call-seq: + * flt.to_s -> string + * flt.inspect -> string + * + * Returns a string containing a representation of self. As well as a + * fixed or exponential form of the number, the call may return + * "<code>NaN</code>", "<code>Infinity</code>", and + * "<code>-Infinity</code>". + * + * 3.0.to_s #=> 3.0 + * 3.25.to_s #=> 3.25 + */ + static mrb_value -flo_to_str(mrb_state *mrb, mrb_value flt, mrb_bool add_dot_zero) +flo_to_s(mrb_state *mrb, mrb_value flt) { mrb_float f = mrb_float(flt); mrb_value str; @@ -293,7 +308,7 @@ flo_to_str(mrb_state *mrb, mrb_value flt, mrb_bool add_dot_zero) str = mrb_float_to_str(mrb, flt, fmt); goto insert_dot_zero; } - else if (add_dot_zero) { + else { mrb_str_cat(mrb, str, ".0", 2); } @@ -305,49 +320,6 @@ flo_to_str(mrb_state *mrb, mrb_value flt, mrb_bool add_dot_zero) return str; } -/* 15.2.9.3.16(x) */ -/* - * call-seq: - * flt.to_s -> string - * - * Returns a string containing a representation of self. As well as a - * fixed or exponential form of the number, the call may return - * "<code>NaN</code>", "<code>Infinity</code>", and - * "<code>-Infinity</code>". - * - * Trailing <code>.0</code> is removed. - * - * 3.0.to_s #=> 3 - * 3.25.to_s #=> 3.25 - */ - -static mrb_value -flo_to_s(mrb_state *mrb, mrb_value flt) -{ - return flo_to_str(mrb, flt, FALSE); -} - -/* - * call-seq: - * flt.inspect -> string - * - * Returns a string containing a representation of self. As well as a - * fixed or exponential form of the number, the call may return - * "<code>NaN</code>", "<code>Infinity</code>", and - * "<code>-Infinity</code>". - * - * Trailing <code>.0</code> is added. - * - * 3.0.to_s #=> 3.0 - * 3.25.to_s #=> 3.25 - */ - -static mrb_value -flo_inspect(mrb_state *mrb, mrb_value flt) -{ - return flo_to_str(mrb, flt, TRUE); -} - /* 15.2.9.3.2 */ /* * call-seq: @@ -1725,7 +1697,7 @@ mrb_init_numeric(mrb_state *mrb) mrb_define_method(mrb, fl, "eql?", flo_eql, MRB_ARGS_REQ(1)); /* 15.2.8.3.16 */ mrb_define_method(mrb, fl, "to_s", flo_to_s, MRB_ARGS_NONE()); /* 15.2.9.3.16(x) */ - mrb_define_method(mrb, fl, "inspect", flo_inspect, MRB_ARGS_NONE()); + mrb_define_method(mrb, fl, "inspect", flo_to_s , MRB_ARGS_NONE()); mrb_define_method(mrb, fl, "nan?", flo_nan_p, MRB_ARGS_NONE()); #ifdef INFINITY |
