C++ 用C/C+将天文数字转换成人类可读的形式+;
我的程序打印出大量的数字——比如100363443,高达一万亿——读起来有点困难,所以我想以易于阅读的形式打印任何数字 现在我用C++ 用C/C+将天文数字转换成人类可读的形式+;,c++,c,largenumber,C++,C,Largenumber,我的程序打印出大量的数字——比如100363443,高达一万亿——读起来有点困难,所以我想以易于阅读的形式打印任何数字 现在我用 printf ("%10ld", number); 格式 我希望使用printf生成一个数字。我的大部分代码是C++,但我不想介绍STD::CUT,因为我已经有了Prtff /P> 谢谢简单的方法可能是在输出之前转换为双精度,并使用%e,它将以指数科学记数法打印它们。试试这个: double n = (double)number; printf("%10.0e",
printf ("%10ld", number);
格式
我希望使用printf生成一个数字。我的大部分代码是C++,但我不想介绍STD::CUT,因为我已经有了Prtff /P>
谢谢简单的方法可能是在输出之前转换为双精度,并使用%e,它将以指数科学记数法打印它们。试试这个:
double n = (double)number;
printf("%10.0e", n);
您可以使用humanize_number(),它使用诸如k、m等后缀来省略低阶数字。这不是一个标准的例程,所以您应该d/l我链接到的源代码。(2条款BSD许可证,允许任何类型的使用。) 从
std::cout记住本地化(特别是在编写库时)。
在欧洲(英国除外),它将是1.000.000而不是1000000使用printf格式字符串中的非标准
撇号,如果您有该选项,并且不介意失去一点可移植性
根据我的文档,自1997年起,”
标志可用于系统
如果您使用的是Unix、Linux、Mac。。。您应该没有问题如果您使用的是Windows、DOS、iSeries、Android。。。所有的赌注都没有了(但也许你可以在你的系统上安装一个POSIX层)
下面是我使用locale编写的一个直接C/o示例。只适用于积极因素。(来自DiscoVlad的大量帮助)
#包括
#包括
#包括
#包括
作废我的U反转(字符*s){
int c,i,j;
对于(i=0,j=strlen(s)-1;i=0;i--){
格式化的[strlen(格式化)]=缓冲区[i];
如果(i%3==0&&i 0){
格式化的[strlen(格式化)]=',';
}
}
自由(缓冲);
返回格式;
}
int main(){
字符*格式化;
//不要忘了在每次通话后释放(格式化)。
格式化=插入逗号(123);
printf(“输出%s\n”,已格式化);
//产出123
格式化=插入逗号(1234);
printf(“输出%s\n”,已格式化);
//产出1234
格式化=插入逗号(123456);
printf(“输出%s\n”,已格式化);
//产出123456
格式化=插入逗号(1234567);
printf(“输出%s\n”,已格式化);
//产出1234567
格式化=插入逗号(123456789);
printf(“输出%s\n”,已格式化);
//产出123456789
格式化=插入逗号(123456789001234567890ULL);
printf(“输出%s\n”,已格式化);
//输出123456789001234567890
}
您希望如何打印您的号码以使其更易于阅读?三个一组的数字?用逗号分隔?空间?我没有严格的要求;我假设逗号以数字3为一组…此功能已经内置到std::ostream对象中。您只需要为流注入正确的设置。请参见下面的Patrick答案。@Martin York粗体显示——我没有使用std::ostream对象……也许只将其重新标记为C问题?这可能更好。我的小把戏只有在你觉得奇怪的情况下才能考虑科学符号“人类可读”。一个很小的讽刺:“前缀”在事物之前(这就是“pre-”的意思)。“后缀”跟在事物后面。110K使用一个K后缀表示110000。只需补充一点,这是一个仅用于整数的函数-不处理浮点数…这是唯一一个答案,完全符合Andrei的要求,使用他想要使用的工具。我得到一个警告-警告#269:无效格式字符串转换:printf(“\n\t%'12ld\n”,总计)@安德烈:很抱歉。显然,您需要打印到一个字符串,格式化该字符串,最后输出格式化字符串。我的意思是,新的格式化字符串类似于Lance Rashing answer。@Andrei-您可能会收到警告,因为撇号修饰符是非标准的。例如,MSVC不支持它。你的编译器给了你一个很好的提示,即使你正在使用的库支持它。这将是查看它们的最简单方式。我唯一要补充的是设置一个断点。任何低于断点的内容,都应采用常规数字格式。是的。使用local是一致的方法。好主意,但您应该更懒惰:std::cout.imbue(std::locale(“”);标准::cout
HUMANIZE_NUMBER(3) NetBSD Library Functions Manual HUMANIZE_NUMBER(3)
NAME
dehumanize_number, humanize_number -- format a number into a human read-
able form and viceversa
SYNOPSIS
#include <stdlib.h>
int
dehumanize_number(const char *str, int64_t *result);
int
humanize_number(char *buf, size_t len, int64_t number,
const char *suffix, int scale, int flags);
Suffix Description Multiplier
k kilo 1024
M mega 1048576
G giga 1073741824
T tera 1099511627776
P peta 1125899906842624
E exa 1152921504606846976
std::cout << std::setprecision(5) << std::scientific << 100363443.0;
struct comma : public std::numpunct<char>
{
protected: std::string do_grouping() const { return "\003" ; }
};
std::cout.imbue( std::locale( std::cout.getloc(), new comma ) );
std::cout << 100363443 << std::endl;
std::cout.imbue( std::locale( "" ) );
std::cout << 100363443 << std::endl;
#include <locale.h>
#include <stdio.h>
int main(void) {
long int x = 130006714000000;
setlocale(LC_NUMERIC, "en_US.utf-8"); /* important */
while (x > 0) {
printf("# %%'22ld: %'22ld\n", x); /* apostrophe flag */
x *= 2; /* on my machine, the Undefined Behaviour for overflow
// makes the number become negative with no ill effects */
}
return 0;
}
# %'22ld: 130,006,714,000,000
# %'22ld: 260,013,428,000,000
# %'22ld: 520,026,856,000,000
# %'22ld: 1,040,053,712,000,000
# %'22ld: 2,080,107,424,000,000
# %'22ld: 4,160,214,848,000,000
# %'22ld: 8,320,429,696,000,000
# %'22ld: 16,640,859,392,000,000
# %'22ld: 33,281,718,784,000,000
# %'22ld: 66,563,437,568,000,000
# %'22ld: 133,126,875,136,000,000
# %'22ld: 266,253,750,272,000,000
# %'22ld: 532,507,500,544,000,000
# %'22ld: 1,065,015,001,088,000,000
# %'22ld: 2,130,030,002,176,000,000
# %'22ld: 4,260,060,004,352,000,000
# %'22ld: 8,520,120,008,704,000,000
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <strings.h>
void my_reverse ( char* s ) {
int c, i, j;
for (i=0, j= strlen(s)-1;i<j;i++,j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
char* insert_commas(unsigned long long input ) {
int i, intlen;
char* buffer;
char* formatted;
intlen = (int) ceil(log10(input * 1.0));
buffer = (char *) malloc((intlen + 1) * sizeof(char));
sprintf(buffer, "%llu", input); // build buffer
formatted = (char *) malloc((intlen + (int) ceil(intlen/3.0)) * sizeof(char)); // malloc output buffer
my_reverse(buffer);
for(i=intlen; i>=0; i--) {
formatted[strlen(formatted)] = buffer[i];
if (i%3 == 0 && i<intlen && i > 0) {
formatted[strlen(formatted)] = ',';
}
}
free(buffer);
return formatted;
}
int main() {
char* formatted;
// don't forget to free(formatted) after each call.
formatted = insert_commas(123);
printf("output %s\n", formatted);
// output 123
formatted = insert_commas(1234);
printf("output %s\n", formatted);
// output 1,234
formatted = insert_commas(123456);
printf("output %s\n", formatted);
// output 123,456
formatted = insert_commas(1234567);
printf("output %s\n", formatted);
// output 1,234,567
formatted = insert_commas(123456789);
printf("output %s\n", formatted);
// output 123,456,789
formatted = insert_commas(12345678901234567890ull);
printf("output %s\n", formatted);
// output 12,345,678,901,234,567,890
}