** 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
| 425 | ** its end. |
| 426 | */ |
| 427 | static int tostringbuffFloat (lua_Number n, char *buff) { |
| 428 | /* first conversion */ |
| 429 | int len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT, |
| 430 | (LUAI_UACNUMBER)n); |
| 431 | lua_Number check = lua_str2number(buff, NULL); /* read it back */ |
| 432 | if (check != n) { /* not enough precision? */ |
| 433 | /* convert again with more precision */ |
| 434 | len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT_N, |
| 435 | (LUAI_UACNUMBER)n); |
| 436 | } |
| 437 | /* looks like an integer? */ |
| 438 | if (buff[strspn(buff, "-0123456789")] == '\0') { |
| 439 | buff[len++] = lua_getlocaledecpoint(); |
| 440 | buff[len++] = '0'; /* adds '.0' to result */ |
| 441 | } |
| 442 | return len; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | /* |