mirror of
https://github.com/janet-lang/janet
synced 2024-06-25 22:53:16 +00:00
Use snprintf(..., "%.17g", x)
to pretty print numbers.
This commit is contained in:
parent
cb25a2ecd6
commit
6b15ed812b
|
@ -40,16 +40,15 @@
|
||||||
|
|
||||||
static void number_to_string_b(JanetBuffer *buffer, double x) {
|
static void number_to_string_b(JanetBuffer *buffer, double x) {
|
||||||
janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2);
|
janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2);
|
||||||
const char *fmt = (x == floor(x) &&
|
|
||||||
x <= JANET_INTMAX_DOUBLE &&
|
|
||||||
x >= JANET_INTMIN_DOUBLE) ? "%.0f" : "%g";
|
|
||||||
int count;
|
int count;
|
||||||
if (x == 0.0) {
|
if (x == 0.0) {
|
||||||
/* Prevent printing of '-0' */
|
/* Prevent printing of '-0' */
|
||||||
count = 1;
|
count = 1;
|
||||||
buffer->data[buffer->count] = '0';
|
buffer->data[buffer->count] = '0';
|
||||||
} else {
|
} else {
|
||||||
count = snprintf((char *) buffer->data + buffer->count, BUFSIZE, fmt, x);
|
/* Use 17 significant digits in the %g format to avoid rounding off the fractional part (if
|
||||||
|
* it exists) and producing a value which looks like an integer. */
|
||||||
|
count = snprintf((char *) buffer->data + buffer->count, BUFSIZE, "%.17g", x);
|
||||||
}
|
}
|
||||||
buffer->count += count;
|
buffer->count += count;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user