Discussion:
how to printf 1 934 210 031 / readable numbers
(too old to reply)
fir
2017-05-01 13:11:25 UTC
Permalink
Raw Message
i mean i want to printf numbers easy to read

1 934 210 031 instead of 1934210031 (terriblu unreadanle)

hw to do that?
Scott Lurndal
2017-05-01 14:31:52 UTC
Permalink
Raw Message
Post by fir
i mean i want to printf numbers easy to read
1 934 210 031 instead of 1934210031 (terriblu unreadanle)
hw to do that?
Use a posix compliant version of the printf family of
functions. Use the apostrophe flag character with
your format specification. Update your locale
database to specify the space character as the
locale-specific thousands grouping character
for the locale active at the time of the snprintf call.

snprintf(buffer, sizeof(buffer), "%'u\n", unsigned_integer_value);
fir
2017-05-01 14:47:57 UTC
Permalink
Raw Message
Post by Scott Lurndal
Post by fir
i mean i want to printf numbers easy to read
1 934 210 031 instead of 1934210031 (terriblu unreadanle)
hw to do that?
Use a posix compliant version of the printf family of
functions. Use the apostrophe flag character with
your format specification. Update your locale
database to specify the space character as the
locale-specific thousands grouping character
for the locale active at the time of the snprintf call.
snprintf(buffer, sizeof(buffer), "%'u\n", unsigned_integer_value);
%'d do not work here (mingw/win32)
bartc
2017-05-01 15:06:33 UTC
Permalink
Raw Message
Post by fir
Post by Scott Lurndal
Post by fir
i mean i want to printf numbers easy to read
1 934 210 031 instead of 1934210031 (terriblu unreadanle)
hw to do that?
Use a posix compliant version of the printf family of
functions. Use the apostrophe flag character with
your format specification. Update your locale
database to specify the space character as the
locale-specific thousands grouping character
for the locale active at the time of the snprintf call.
snprintf(buffer, sizeof(buffer), "%'u\n", unsigned_integer_value);
%'d do not work here (mingw/win32)
You might need to do some coding. Example:

#include <stdio.h>
#include <stdint.h>

int u64tostr (uint64_t aa, char * dest, int destsize, int sep) {
char t[360];
int i;
int j;
int k;
int g;
char* s;
if (destsize<=0) return 0;
i = 0; // total chars in output
k = 0; // chars in group
g = 3; // separators every 3 digits
do {
t[++i] = aa%10+'0';
aa = aa/10;
if (sep && aa != 0 && ++k == g) {
t[++i] = sep;
k = 0;
}
} while (aa);
j = i;
s = dest;
if (i<destsize) {
while (i) {
*s = t[i--];
++s;
}
}
else {
i=destsize-1;
while (i--) *s++='#';
}
*s = 0;
return j; //return total chars in number
}

int i64tostr (int64_t aa, char * dest, ssize_t destsize, int sep) {
if (aa<0) {
*dest='-';
return u64tostr(-aa,dest+1,destsize-1,sep);
}
else
return u64tostr(aa,dest,destsize,sep);

}

void println_i64(int64_t aa){
char str[100];
int n;
i64tostr(aa,str,sizeof(str),',');
puts(str);
}

int main(void) {

println_i64(1);
println_i64(12);
println_i64(123);
println_i64(1234);
println_i64(12345);
println_i64(123456);
println_i64(1234567);
println_i64(12345678);
println_i64(123456789);
println_i64(1234567890);
println_i64(1000000000000000000);

}
fir
2017-05-01 15:59:19 UTC
Permalink
Raw Message
Post by bartc
Post by fir
Post by Scott Lurndal
Post by fir
i mean i want to printf numbers easy to read
1 934 210 031 instead of 1934210031 (terriblu unreadanle)
hw to do that?
Use a posix compliant version of the printf family of
functions. Use the apostrophe flag character with
your format specification. Update your locale
database to specify the space character as the
locale-specific thousands grouping character
for the locale active at the time of the snprintf call.
snprintf(buffer, sizeof(buffer), "%'u\n", unsigned_integer_value);
%'d do not work here (mingw/win32)
#include <stdio.h>
#include <stdint.h>
int u64tostr (uint64_t aa, char * dest, int destsize, int sep) {
char t[360];
int i;
int j;
int k;
int g;
char* s;
if (destsize<=0) return 0;
i = 0; // total chars in output
k = 0; // chars in group
g = 3; // separators every 3 digits
do {
t[++i] = aa%10+'0';
aa = aa/10;
if (sep && aa != 0 && ++k == g) {
t[++i] = sep;
k = 0;
}
} while (aa);
j = i;
s = dest;
if (i<destsize) {
while (i) {
*s = t[i--];
++s;
}
}
else {
i=destsize-1;
while (i--) *s++='#';
}
*s = 0;
return j; //return total chars in number
}
int i64tostr (int64_t aa, char * dest, ssize_t destsize, int sep) {
if (aa<0) {
*dest='-';
return u64tostr(-aa,dest+1,destsize-1,sep);
}
else
return u64tostr(aa,dest,destsize,sep);
}
void println_i64(int64_t aa){
char str[100];
int n;
i64tostr(aa,str,sizeof(str),',');
puts(str);
}
int main(void) {
println_i64(1);
println_i64(12);
println_i64(123);
println_i64(1234);
println_i64(12345);
println_i64(123456);
println_i64(1234567);
println_i64(12345678);
println_i64(123456789);
println_i64(1234567890);
println_i64(1000000000000000000);
}
this code it yourself (revritting it into new buffer and putting spaces) seems not quite ideal solution (for some, probably numerous, reasons).. but if no other way probably i would need to do it such way :<<
bartc
2017-05-01 16:03:27 UTC
Permalink
Raw Message
Post by bartc
int u64tostr (uint64_t aa, char * dest, int destsize, int sep) {
int i64tostr (int64_t aa, char * dest, ssize_t destsize, int sep) {
destsize should have been int to match the other (it's better behaved as
int). But it shouldn't even have been ssize_t, but size_t. I'm not sure
what ssize_t is supposed to be, but it slipped through as gcc didn't
complain; other compilers did.
--
bartc
Scott Lurndal
2017-05-01 18:43:44 UTC
Permalink
Raw Message
Post by bartc
Post by bartc
int u64tostr (uint64_t aa, char * dest, int destsize, int sep) {
int i64tostr (int64_t aa, char * dest, ssize_t destsize, int sep) {
destsize should have been int to match the other (it's better behaved as
int). But it shouldn't even have been ssize_t, but size_t. I'm not sure
what ssize_t is supposed to be, but it slipped through as gcc didn't
complain; other compilers did.
ssize_t covers the domain [-1, SSIZE_MAX]. It's intended to be
used by posix for system calls that return a +ve unsigned integer
but also need to be able to return -1 to indicate error.
Scott Lurndal
2017-05-01 18:40:59 UTC
Permalink
Raw Message
W dniu poniedzia=C5=82ek, 1 maja 2017 16:31:59 UTC+2 u=C5=BCytkownik Scott =
Post by Scott Lurndal
Post by fir
i mean i want to printf numbers easy to read
1 934 210 031 instead of 1934210031 (terriblu unreadanle)
hw to do that?
=20
Use a posix compliant version of the printf family of
functions. Use the apostrophe flag character with
your format specification. Update your locale
database to specify the space character as the
locale-specific thousands grouping character
for the locale active at the time of the snprintf call.
=20
snprintf(buffer, sizeof(buffer), "%'u\n", unsigned_integer_value);
%'d do not work here (mingw/win32)
It should - check your locale. The "C" locale doesn't define
a thousands grouping character, so you won't see any affect with
the apostrophe flag.

However, as you're working with windows, all bets are off.
Real Troll
2017-05-01 20:49:00 UTC
Permalink
Raw Message
Post by Scott Lurndal
However, as you're working with windows, all bets are off.
In VS C++ this should work:


array<Decimal>^ amounts = {static_cast<Decimal>(16305.32),
static_cast<Decimal>(18794.16) };
Console::WriteLine(" Beginning Balance Ending Balance");
Console::WriteLine(" {0,-28:C2}{1,14:C2}", amounts[0], amounts[1]);
// Displays:
// Beginning Balance Ending Balance
// $16,305.32 $18,794.16

Loading...