MCPcopy Create free account
hub / github.com/GenericMappingTools/gmt / gmt_sprintf_float

Function gmt_sprintf_float

src/gmt_support.c:12104–12173  ·  view source on GitHub ↗

! . */

Source from the content-addressed store, hash-verified

12102
12103/*! . */
12104void gmt_sprintf_float (struct GMT_CTRL *GMT, char *string, char *format, double x) {
12105 /* Determines if %-apostrophe is used in the format for a float. If so, must temporarily switch to LC_NUMERIC=en_US */
12106 char *use_locale = strstr (format, "%'");
12107#ifdef HAVE_SETLOCALE
12108 if (use_locale) setlocale (LC_NUMERIC, "en_US");
12109#endif
12110 if (GMT->current.plot.substitute_pi) { /* Want to use pi when close to known multiples of pi. This variable is set per axis in gmt_xy_axis */
12111 /* Any fraction of pi up to 1/20th allowed.
12112 * substitute_pi becomes true when items with "pi" are used in -R, -I etc. */
12113 int k = 1, m, n;
12114 char fmt[GMT_LEN16] = {""};
12115 double f = fabs (x / M_PI); /* Float multiples of pi */
12116 if (fabs (f) < GMT_CONV4_LIMIT) { /* Deal with special case of zero pi */
12117 sprintf (string, "0");
12118 return;
12119 }
12120 gmtsupport_make_fraction (GMT, f, 20, &n, &m); /* Max 20th of pi */
12121 string[0] = (x < 0.0) ? '-' : '+'; /* Place leading sign */
12122 string[1] = '\0'; /* Chop off old string */
12123 if (n > 1) { /* Need an integer in front of pi */
12124 k += snprintf (fmt, GMT_LEN16, "%d", n);
12125 strcat (string, fmt);
12126 //k += irint (floor (log10 ((double)n))) + 1; /* Add how many decimals needed for n */
12127 }
12128 strcat (string, "@~p@~"); /* Place the pi symbol */
12129 k += 5; /* Add number of characters just placed to print pi */
12130 if (m > 1) { /* Add the fractions part */
12131 k += snprintf (fmt, GMT_LEN16, "/%d", m);
12132 strcat (string, fmt);
12133 //k += irint (floor (log10 ((double)m))) + 2; /* Add how many decimals needed for /m */
12134 }
12135 string[k] = '\0'; /* Truncate any old string */
12136 //fprintf (stderr, "f = %g n = %d m = %d. String = %s\n", f, n, m, string);
12137 }
12138 else
12139#ifndef WIN32
12140 sprintf (string, format, x);
12141#else
12142 { /* Windows doesn't support %' format */
12143 if (use_locale) {
12144 char *new_format = gmt_strrep(format, "%'", "%");
12145 sprintf (string, new_format, x);
12146 gmt_M_str_free (new_format);
12147 } else
12148 sprintf (string, format, x);
12149 }
12150#endif
12151 if (use_locale) {
12152#ifdef HAVE_SETLOCALE
12153 setlocale (LC_NUMERIC, "C"); /* Undo the damage */
12154#endif
12155 if (strchr (string, ',') == NULL && fabs (x) > 1000.0 && fabs (x - irint (x)) < GMT_CONV8_LIMIT) {
12156 /* System not capable of printf groups for integers so we insert those commas manually... */
12157 char *tmp = strdup (string);
12158 int n = 0, olen = (int)strlen (tmp), k = (x < 0) ? 1 : 0;
12159 int nlen = olen + irint (floor (log10(fabs(x))/3.0)); /* Number of commas added */
12160 string[nlen] = '\0';
12161 /* We guild the string from back to front */

Callers 5

gmt_draw_map_scaleFunction · 0.85
gmtsupport_label_is_OKFunction · 0.85
gmtlib_get_annot_labelFunction · 0.85
psscale_draw_colorbarFunction · 0.85

Calls 4

gmtsupport_make_fractionFunction · 0.85
strdupFunction · 0.85
GMT_ReportFunction · 0.85
gmt_strrepFunction · 0.70

Tested by

no test coverage detected