What I've resorted to is this, but I don't like it:
char buf[100];
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDIGITS, buf, sizeof buf);
int nDecimalDigits = atoi(buf);
sprintf(buf, "%d", value);
GetNumberFormat(LOCALE_USER_DEFAULT, 0, strSrc, 0, buf, sizeof buf);
// chop off decimal portion
cp[strlen(cp) - nDecimalDigits - 1] = 0;
Your method is more concise than the one I used Mark. I too was
suprised that there is no easy way of printing integer numbers without
decimal places. Here's what I'd used:
char szFileSize[ 15 ];
_itoa( 123456, szFileSize, 10 );
static NUMBERFMT nf;
/* Only do this once! It's rather long winded */
if ( nf.lpThousandSep == NULL )
{
char szBuffer[5];
static char szDecSep[5];
static char szThousandsSep[5];
GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_SDECIMAL,
&szDecSep[0], sizeof( szDecSep ) );
GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_STHOUSAND,
&szThousandsSep[0],
sizeof( szThousandsSep ) );
/* We want no fractions */
nf.NumDigits = 0;
/* But all the system defaults for the others */
GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_ILZERO,
&szBuffer[0], sizeof( szBuffer ) );
nf.LeadingZero = atoi( szBuffer );
GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_SGROUPING,
&szBuffer[0], sizeof( szBuffer ) );
nf.Grouping = atoi( szBuffer );
nf.lpDecimalSep = szDecSep;
nf.lpThousandSep = szThousandsSep;
GetLocaleInfo( LOCALE_USER_DEFAULT, LOCALE_INEGNUMBER,
&szBuffer[0], sizeof( szBuffer ) );
nf.NegativeOrder = atoi( szBuffer );
}
/* Convert the number string into the proper locale defn */
GetNumberFormat( LOCALE_USER_DEFAULT, 0, szFileSize,
&nf, szSize, sizeof( szSize ) );
Dave
----
Address is altered to discourage junk mail.
Remove ".---" for the real address.
Your code is better because it handles the case of the negative sign going
at the end of the number.
Oh well, glad you found it useful Mark. Tedious to have to do it
though, I'd hoped there was a neater way.
David Lowndes <dav...@mvps.org.---> wrote in article
That's the point--there must have been _some_ reason for this design. Sure
be curious to know what it was, though.
> Still that's the way it is!
Quite right. I only bitch in hopes that someone will explain the behavior.
:)
Yes, that struck me as being too long winded to imagine anyone
willingly designing it that way. Still that's the way it is!
Cheers
To keep them from having to accept "float" or "double" parameters, with the
attending format problems (64-bit? 80-bit?) that would cause, I imagine.
Fundamentally, they're text-formatting APIs. There's no call to go linking FP
support into the API just to move some decimal points and commas around in a
string.
--
Ben Goetter, Angry Graycat Designs
http://www.angrygraycat.com/goetter/
Surely in this day and age where floating point support is builtin to the
CPU, double is not exactly an esoteric format?
> Fundamentally, they're text-formatting APIs. There's no call to go
linking FP
> support into the API just to move some decimal points and commas around
in a
> string.
Well, to use them, one is obviously going to have to link FP support in
somewhere. These functions aren't just "text formatting" functions--they're
functions to format text that has been formatted from numbers. At some
point, you have to deal with numbers--my question is why these functions
require a two-step process?