Skip to content

Commit

Permalink
Fixed floating point number formatting to match MOO formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
toddsundsted committed Jun 13, 2011
1 parent 0a10fd9 commit 9ae24d4
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion yajl_gen.c
Expand Up @@ -37,6 +37,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <float.h>
#include <math.h>

typedef enum {
Expand Down Expand Up @@ -219,14 +220,29 @@ yajl_gen_integer(yajl_gen g, long int number)
#define isinf !_finite
#endif

/* Pulled from streams.c for consistent formatting of floating point
* numbers. */
static const char *
dbl_fmt(void)
{
static const char *fmt = NULL;
static char buffer[10];

if (!fmt) {
sprintf(buffer, "%%.%dg", DBL_DIG);
fmt = buffer;
}
return fmt;
}

yajl_gen_status
yajl_gen_double(yajl_gen g, double number)
{
char i[32];
ENSURE_VALID_STATE; ENSURE_NOT_KEY;
if (isnan(number) || isinf(number)) return yajl_gen_invalid_number;
INSERT_SEP; INSERT_WHITESPACE;
sprintf(i, "%.20g", number);
sprintf(i, dbl_fmt(), number);
g->print(g->ctx, i, (unsigned int)strlen(i));
APPENDED_ATOM;
FINAL_NEWLINE;
Expand Down

0 comments on commit 9ae24d4

Please sign in to comment.