** Convert a float to a string, adding it to a buffer. First try with ** a not too large number of digits, to avoid noise (for instance, ** 1.1 going to "1.1000000000000001"). If that lose precision, so ** that reading the result back gives a different number, then do the ** conversion again with extra precision. Moreover, if the numeral looks ** like an integer (without a decimal point or an expo
| 457 | ** its end. |
| 458 | */ |
| 459 | static int tostringbuffFloat (lua_Number n, char *buff) { |
| 460 | /* first conversion */ |
| 461 | int len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT, |
| 462 | (LUAI_UACNUMBER)n); |
| 463 | lua_Number check = lua_str2number(buff, NULL); /* read it back */ |
| 464 | if (check != n) { /* not enough precision? */ |
| 465 | /* convert again with more precision */ |
| 466 | len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT_N, |
| 467 | (LUAI_UACNUMBER)n); |
| 468 | } |
| 469 | /* looks like an integer? */ |
| 470 | if (buff[strspn(buff, "-0123456789")] == '\0') { |
| 471 | buff[len++] = lua_getlocaledecpoint(); |
| 472 | buff[len++] = '0'; /* adds '.0' to result */ |
| 473 | } |
| 474 | return len; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | /* |